iOS中SQLite3数据库修改字段名
關(guān)于數(shù)據(jù)庫(kù)的增、刪、改、查等基本操作本文不去介紹,網(wǎng)上大把的詳細(xì)教程,這里只介紹實(shí)際使用中的一種情況:數(shù)據(jù)庫(kù)中修改字段名稱(chēng)。
在產(chǎn)品更迭的過(guò)程中我們有時(shí)候需要修改數(shù)據(jù)庫(kù)表中已存在的字段名或字段類(lèi)型,這個(gè)時(shí)候我們?nèi)aidu.com或jianshu.com的話(huà)找到的答案應(yīng)該是以下兩種:
- alter table user CHANGE new1 new4 int;
- alter table user rename column a to b;
然而這兩種方法都并不能在SQLite3中起作用,因?yàn)樵?SQLite 中,除了重命名表和在已有的表中添加列,ALTER TABLE 命令不支持其他操作。也就是說(shuō),SQLite無(wú)法使用rename/change語(yǔ)句修改字段名了。但如果我們產(chǎn)品更迭過(guò)程中不巧有這種奇葩需求了,怎么辦呢?這時(shí)候只能采取迂回政策了:
如何把tableA中的字段oldColumn修改為newColumn
解決方法:
第3步解決辦法:(依舊是迂回政策)
3.1. 先創(chuàng)建備份表tableA_backup將tableA數(shù)據(jù)備份
3.2 將原表tableA中的數(shù)據(jù)對(duì)應(yīng)的復(fù)制到tableA_backup表中
INSERT INTO tableA_backup SELECT id, newColumn FROM tableA 復(fù)制代碼不要將舊字段oldColumn復(fù)制過(guò)去,備份表tableA_backup中沒(méi)有oldColumn字段
3.3 此時(shí)實(shí)際已經(jīng)得到了改過(guò)字段名的表:tableA_backup,然后將原表tableA刪掉
3.4 然后創(chuàng)建表tableA
CREATE TABLE tableA(id INTEGER PRIMARY KEY AUTOINCREMENT, newColumn INTEGER) 復(fù)制代碼3.5 再把備份表tableA_backup中的數(shù)據(jù)復(fù)制到tableA中
INSERT INTO tableA SELECT id, newColumn FROM tableA_backup 復(fù)制代碼3.6 把備份表tableA_backup刪除,Have done!
DROP TABLE tableA_backup 復(fù)制代碼修改字段名及類(lèi)型的思路就是上面這些步驟,下面給貼上代碼方便大家閱讀使用: # 方法調(diào)用:[self alterTable:@"currentList"renameOldColumn:@"isvipa"toNewColumn:@"vipType"andNewColumnType:@"INTEGER"withDatabase:dballRenamedColumnsAndTypes:@"id INTEGER PRIMARY KEY AUTOINCREMENT, isinsider INTEGER, vipType INTEGER, groupcode TEXT, groupname TEXT, grouppicpath TEXT, uuid TEXT, AESkey TEXT"allRenamedColumnNames:@"id, isinsider, vipType, groupcode, groupname, grouppicpath, uuid, AESkey"];復(fù)制代碼/**修改表中一個(gè)字段名 (sqlite3無(wú)法使用rename/change語(yǔ)句直接修改字段名)@param tableName 表名@param oldColumnName 要被修改的字段名@param newColumnName 字段被修改后的名稱(chēng)@param newColumnType 字段被修改后的類(lèi)型@param db 數(shù)據(jù)庫(kù)對(duì)象@param renamedColumnsAndTypes 修改后所有的字段及對(duì)應(yīng)的類(lèi)型 eg: id INTEGER PRIMARY KEY AUTOINCREMENT, isinsider INTEGER, vipType INTEGER, groupcode TEXT, groupname TEXT, grouppicpath TEXT, uuid TEXT, AESkey TEXT@param renamedColumnNames 修改后所有的字段名 eg: id, isinsider, vipType, groupcode, groupname, grouppicpath, uuid, AESkey FROM currentList*/ -(void)alterTable:(NSString *)tableNamerenameOldColumn:(NSString *)oldColumnName toNewColumn:(NSString *)newColumnName andNewColumnType:(NSString *)newColumnTypewithDatabase:(FMDatabase *)db allRenamedColumnsAndTypes:(NSString *)renamedColumnsAndTypes allRenamedColumnNames:(NSString *)renamedColumnNames {if (![db columnExists:oldColumnName inTableWithName:tableName])return;DLog(@"%@表中存在%@字段,修改%@字段名稱(chēng)為%@", tableName, oldColumnName, oldColumnName, newColumnName);// 1.添加新字段if (![db executeUpdate:[NSString stringWithFormat:@"ALTER TABLE %@ ADD COLUMN %@ %@", tableName, newColumnName, newColumnType]]) {DLog(@"%@修改字段:添加新字段%@失敗", tableName, newColumnName);}// 2.將舊字段賦值給新字段if (![db executeUpdate:[NSString stringWithFormat:@"UPDATE %@ SET %@ = %@", tableName, newColumnName, oldColumnName]]) {DLog(@"%@修改字段:%@賦值%@字段失敗", tableName, oldColumnName, newColumnName);return;}// 3.刪除舊字段(sqlite3無(wú)法使用DROP刪除字段,先復(fù)制備份,再創(chuàng)建新的表)if (![db executeUpdate:@"BEGIN TRANSACTION"]) {DLog(@"BEGIN TRANSACTION失敗");}NSString *table_backup = [NSString stringWithFormat:@"%@_backup", tableName];NSString *sql = [NSString stringWithFormat:@"CREATE TEMPORARY TABLE %@(%@)", table_backup, renamedColumnsAndTypes];if (![db executeUpdate:sql]) {DLog(@"CREATE TEMPORARY TABLE %@ 失敗", table_backup);}if (![db executeUpdate:[NSString stringWithFormat:@"INSERT INTO %@ SELECT %@ FROM %@", table_backup, renamedColumnNames, tableName]]) {DLog(@"INSERT INTO %@ 失敗", table_backup);}if (![db executeUpdate:[NSString stringWithFormat:@"DROP TABLE %@", tableName]]) {DLog(@"DROP TABLE %@ 失敗", tableName);}if (![db executeUpdate:[NSString stringWithFormat:@"CREATE TABLE %@(%@)", tableName, renamedColumnsAndTypes]]) {DLog(@"CREATE TABLE %@ 失敗", tableName);}if (![db executeUpdate:[NSString stringWithFormat:@"INSERT INTO %@ SELECT %@ FROM %@", tableName, renamedColumnNames, table_backup]]) {DLog(@"INSERT INTO %@ SELECT FROM %@ 失敗", tableName, table_backup);}if (![db executeUpdate:[NSString stringWithFormat:@"DROP TABLE %@", table_backup]]) {DLog(@"DROP TABLE %@ 失敗", table_backup);}if (![db executeUpdate:@"COMMIT"]) {DLog(@"COMMIT失敗");} } 復(fù)制代碼
總結(jié)
以上是生活随笔為你收集整理的iOS中SQLite3数据库修改字段名的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: The hierarchy of the
- 下一篇: 命令行下从bak文件恢复sqlserve