How to count number of columns in a table in SQLITE?

后端 未结 4 1575
我寻月下人不归
我寻月下人不归 2021-01-13 03:59

How can I count the number of columns in a table in a sqlite database in Android?

4条回答
  •  感动是毒
    2021-01-13 04:48

    Here you go!

    -(NSInteger)dbFieldCount:(NSString *)dbname
    {
        NSString *query = [NSString stringWithFormat:@"PRAGMA table_info(%@)",dbname];
        const char *query2 = [query UTF8String];
        NSInteger nFields =0;
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, query2, -1, &compiledStatement, NULL) == SQLITE_OK) 
        {
             while(sqlite3_step(compiledStatement) == SQLITE_ROW)
             {
                   nFields++;
             }
        }
        sqlite3_finalize(compiledStatement);
        return nFields;
    }
    

提交回复
热议问题