用心计较般般错 安心自守事事宽

cgl 坚持、努力终有所获
  新随笔  :: 管理

SQlite如何检测表是否创建

Posted on 2013-01-25 13:56  用心计较般般错 安心自守事事宽  阅读(4017)  评论(0编辑  收藏  举报

PDA中设计数据库后,避免不了的会根据实际的需求改动功能。最害怕的是,PAD都发下去了,很多台,而工作已经开展了,数据已经有一部分了,中间要增加功能,增加表或者增加字段,怎么办?只有在更新程序的时候,判断表是否存在,不存在则临时创建,字段也类似。

  Sqlite中判断表是否存在,表名'attPointSampleEx'

            string sql = "SELECT COUNT(*)  as CNT FROM sqlite_master where type='table' and name='attPointSampleEx'";

            string sql1 = "CREATE TABLE [attPointSampleEx] ([ID] VARCHAR(50) NOT NULL UNIQUE,[PickName] VARCHAR(20)," +

              "[SpecimenCode] VARCHAR(200), " +

              "[PickTime] VARCHAR(19)," +

              "[PickAddre] VARCHAR(200)," +

              "[Lon] VARCHAR(20)," +

              "[ZYName] VARCHAR(200), " +

              "[ZYCode] VARCHAR(10), " +

              "[PlantXue] VARCHAR(200), " +

              "[HerbalPart] VARCHAR(50), " +

              "[YongTu] VARCHAR(50), " +

              "[SampleType] VARCHAR(2)," +

              "[LiYong] VARCHAR(200)," +

              "[WeiXie] VARCHAR(200)," +

              "[Notion] VARCHAR(200));";

            //_IsCreate Bool类型,判断是否创建了表

            if (!_IsCreate)

            {

                //读取系统表,看是否有此表的记录

                DataTable tDT = MainDataBase.pMainDataBase.pDataBase.getDataTableFromSql(sql);

                if (tDT != null && tDT.Rows.Count>0)

                {

                    //判断记录

                    _IsCreate = Convert.ToInt32(tDT.Rows[0][0]) == 1;

                    if (!_IsCreate)

                    {

                        //如果没有记录,则创建表

                        _IsCreate = MainDataBase.pMainDataBase.pDataBase.ExecuteNonQuery(sql1) >= 0;

                    }

                    tDT.Dispose();

                }

            }