1.判斷數(shù)據(jù)庫版本號(hào)和保存數(shù)據(jù)庫版本號(hào)
NSString* constkdbManagerVersion =@"DBManagerVersion";
const static NSIntegerDB_MANAGER_VER =1;
保存新版本號(hào)存儲(chǔ)到NSUserDefaults中
2.數(shù)據(jù)庫升級(jí)
// 獲取舊版本號(hào)
// 升級(jí)
// 判斷是否需要升級(jí)
// 執(zhí)行版本1到版本2的更新
3.數(shù)據(jù)庫升級(jí)的SQL語句操作
// 變更數(shù)據(jù)庫表為一個(gè)舊數(shù)據(jù)表
NSString*sqlStr = [NSString stringWithFormat:@"ALTER TABLE %@ RENAME TO %@",self.tableName, [self.tableName stringByAppendingString:@"_Old"]
// 執(zhí)行SQL語句操作
[db executeUpdate:sqlStr];
// 創(chuàng)建新的數(shù)據(jù)表
NSString*executeStr = [NSString stringWithFormat:@"create table if not exists %@ (LocID integer primary key autoincrement not null,messageID text unique,Content text,TypeName text,SendTime text,CreateTime integer,Status integer,msgtype text,apply_id text,userid text,message_last_id text)",self.tableName];
// 從舊數(shù)據(jù)表把舊數(shù)據(jù)插入新的數(shù)據(jù)表中
// 刪除舊的數(shù)據(jù)表