iOS-SQLite(FMDB)

在已经存在的表中,添加字段,更新表结构

/** 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;

举例说明

    
    // 在 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];
        }
    }];

posted @ 2017-05-05 10:14  ShaoYL  阅读(130)  评论(0编辑  收藏  举报