在已经存在的表中,添加字段,更新表结构
代码语言:javascript
复制/** Test to see if particular column exists for particular table in database
@param columnName The name of the column.
@param tableName The name of the table.
@return `YES` if column exists in table in question; `NO` otherwise.
*/
- (BOOL)columnExists:(NSString*)columnName inTableWithName:(NSString*)tableName;
举例说明
代码语言:javascript
复制 // 在 testTable 中,添加 name 字段
[_dbQueue inDatabase:^(FMDatabase *db) {
NSString *createTable = [NSString stringWithFormat:@"create table if not exists testTable (sessionId text primary key not null, wds text);"];
BOOL rezult = [db executeUpdate:createTable];
if (![db columnExists:@"name" inTableWithName:@"testTable"]){
NSString * Show_Notice = [NSString stringWithFormat:@"ALTER TABLE testTable ADD COLUMN name text;"];
[db executeUpdate:Show_Notice];
}
}];