查询SQLite数据库表中字段(列)存在的方法
使用SQL语句为:PRAGMA table_info([DeviceTrees]); 其中“DeviceTrees”为数据库表的名称。
使用SQLite Expert Professional工具,查看该语句是否起作用,这里使用的版本是v3.1.9的。
输入语句后,点击“Execute SQL”按钮后,查询结果如下图所示,说明该语句查询是正确的。

在C# 中通过该语句查询DeviceTrees表中所有字段(列),放入DataTable中,通过循环判读字段(列)是否存在。示例代码如下所示
public  bool InsertColumnGuidFunc()
{
	bool isExist = false;
	try
	{
		List<string> columnList = new List<string>();
		//查询DetectorTrees 表信息
		string strSql = "PRAGMA table_info([DeviceTrees]);";
        //调用SQLite数据库接口
		using (DataTable dt = SQLiteDbHelper.ExecuteDataTable(strSql.ToString(), null))
		{
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					string columnName = dt.Rows[i]["name"].ToString();
					columnList.Add(columnName);
				}
			}
		}
		//判断列是否存在
		if (columnList != null && columnList.Count > 0)
		{
			if (columnList.Contains("DeviceGuid"))
				isExist = true;
			else
				isExist = false;
		}
	}
	catch (Exception ex)
	{
	}
	finally
	{
	}
	return isExist;
}
根据查询结果,如果字段(列)存在,则不进行插入字段(列)的操作,反之,则可通过sql语句向表中插入想要的字段(列)。
扩展
插入字段(列)主要语句为:
alter table DeviceTrees add column [DeviceGuid] VARCHAR(100) NOT NULL DEFAULT ('00000000-0000-0000-0000-000000000000');**************************************************************************************************************



















