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

后端 未结 18 1237
半阙折子戏
半阙折子戏 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:25

    I know it's too late but this will help other.

    To find the column name of the table, you should execute select * from tbl_name and you will get the result in sqlite3_stmt *. and check the column iterate over the total fetched column. Please refer following code for the same.

    // sqlite3_stmt *statement ;
    int totalColumn = sqlite3_column_count(statement);
    for (int iterator = 0; iterator<totalColumn; iterator++) {
       NSLog(@"%s", sqlite3_column_name(statement, iterator));
    }
    

    This will print all the column names of the result set.

    0 讨论(0)
  • 2020-11-27 09:26
    PRAGMA table_info(table_name);
    

    will get you a list of all the column names.

    0 讨论(0)
  • 2020-11-27 09:26

    When you run the sqlite3 cli, typing in:

    sqlite3 -header
    

    will also give the desired result

    0 讨论(0)
  • 2020-11-27 09:29

    Maybe you just want to print the table headers on the console. This is my code: (for each table)

        // ------------------ show header ----------------
    
    
        char sqlite_stmt_showHeader[1000];
        snprintf(sqlite_stmt_showHeader, 1000, "%s%s", "SELECT * FROM ", TABLE_NAME_STRING UTF8String]);
    
        sqlite3_stmt* statement_showHeader;
        sqlite3_prepare_v2(DATABASE, sqlite_stmt_showHeader, -1, &statement_showHeader, NULL);
    
        int headerColumnSize = sqlite3_column_count(statement_showHeader);
    
        NSString* headerRow = @"|";
    
        for (int j = 0; j < headerColumnSize; j++) {
            NSString* headerColumnContent = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_name(statement_showHeader, j)];
            headerRow = [[NSString alloc] initWithFormat:@"%@ %@ |", headerRow, headerColumnContent];
        }
    
        NSLog(@"%@", headerRow);
    
    
        sqlite3_finalize(statement_showHeader);
    
        // ---------------- show header end ---------------------
    
    0 讨论(0)
  • 2020-11-27 09:33

    Just for super noobs like me wondering how or what people meant by

    PRAGMA table_info('table_name') 
    

    You want to use use that as your prepare statement as shown below. Doing so selects a table that looks like this except is populated with values pertaining to your table.

    cid         name        type        notnull     dflt_value  pk        
    ----------  ----------  ----------  ----------  ----------  ----------
    0           id          integer     99                      1         
    1           name                    0                       0
    

    Where id and name are the actual names of your columns. So to get that value you need to select column name by using:

    //returns the name
    sqlite3_column_text(stmt, 1);
    //returns the type
    sqlite3_column_text(stmt, 2);
    

    Which will return the current row's column's name. To grab them all or find the one you want you need to iterate through all the rows. Simplest way to do so would be in the manner below.

    //where rc is an int variable if wondering :/
    rc = sqlite3_prepare_v2(dbPointer, "pragma table_info ('your table name goes here')", -1, &stmt, NULL);
    
    if (rc==SQLITE_OK)
    {
        //will continue to go down the rows (columns in your table) till there are no more
        while(sqlite3_step(stmt) == SQLITE_ROW)
        {
            sprintf(colName, "%s", sqlite3_column_text(stmt, 1));
            //do something with colName because it contains the column's name
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:33

    .schema table_name

    This will list down the column names of the table from the database.

    Hope this will help!!!

    0 讨论(0)
提交回复
热议问题