How to get a list of column names on Sqlite3 database?

后端 未结 18 1366
半阙折子戏
半阙折子戏 2020-11-27 08:48

I want to migrate my iPhone app to a new database version. Since I don\'t have some version saved, I need to check if certain column names exist.

This Stackoverflow

18条回答
  •  借酒劲吻你
    2020-11-27 09:23

    //JUST little bit modified the answer of giuseppe  which returns array of table columns
    +(NSMutableArray*)tableInfo:(NSString *)table{
    
        sqlite3_stmt *sqlStatement;
    
        NSMutableArray *result = [NSMutableArray array];
    
        const char *sql = [[NSString stringWithFormat:@"PRAGMA table_info('%@')",table] UTF8String];
    
        if(sqlite3_prepare(md.database, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    
        {
            NSLog(@"Problem with prepare statement tableInfo %@",
                    [NSString stringWithUTF8String:(const char *)sqlite3_errmsg(md.database)]);
    
        }
    
        while (sqlite3_step(sqlStatement)==SQLITE_ROW)
        {
            [result addObject:
              [NSString stringWithUTF8String:(char*)sqlite3_column_text(sqlStatement, 1)]];
        }
    
        return result;
    }
    

提交回复
热议问题