private void InitDataBase(int version) { int result = db.ExecuteScalar("SELECT count(*) from sqlite_master where type='table' and name='" + TABLE_NAME + "'"); if (result == 0) { //表不存在 string[] colNames = { "KEY", "VALUE" }; string[] colTypes = { "TEXT", "TEXT" }; db.CreateTable(TABLE_NAME, colNames, colTypes); db.InsertValues(TABLE_NAME, new string[] { "version", version + "" }); } else { //判断数据库是否需要升级 DataTable dt = db.GetTable("select * from " + TABLE_NAME + " where KEY='version'"); if (dt != null && dt.Rows.Count > 0) { int OldVersion = Convert.ToInt32(dt.Rows[0]["VALUE"]); if (OldVersion < version) { UpdateDB(); db.UpdateValues(TABLE_NAME, new string[] { "VALUE" }, new string[] { version + "" }, "KEY", "version", "="); } } } CreateAllTables(); }
public static void UpdateTable(SqLiteHelper db) { int result = db.ExecuteScalar("SELECT count(*) from sqlite_master where type='table' and name='" + TABLE_NAME + "'"); if (result != 0) { //db.ExecuteScalar(" DROP TABLE " + TABLE_NAME); string Template = "ALTER TABLE {0} ADD {1} {2}"; StringBuilder sb = new StringBuilder(); sb.AppendFormat(Template, TABLE_NAME, "MsgId", "TEXT"); db.ExecuteSql(sb.ToString()); } //CreateTable(db); }