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

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

    To get a list of columns you can simply use:

    .schema tablename
    
    0 讨论(0)
  • 2020-11-27 09:35

    you can use Like statement if you are searching for any particular column

    ex:

    SELECT * FROM sqlite_master where sql like('%LAST%')
    
    0 讨论(0)
  • 2020-11-27 09:43

    If you want the output of your queries to include columns names and be correctly aligned as columns, use these commands in sqlite3:

    .headers on
    .mode column
    

    You will get output like:

    sqlite> .headers on
    sqlite> .mode column
    sqlite> select * from mytable;
    id          foo         bar
    ----------  ----------  ----------
    1           val1        val2
    2           val3        val4
    
    0 讨论(0)
  • 2020-11-27 09:45

    I know it is an old thread, but recently I needed the same and found a neat way:

    SELECT c.name FROM pragma_table_info('your_table_name') c;
    
    0 讨论(0)
  • 2020-11-27 09:45
    -(NSMutableDictionary*)tableInfo:(NSString *)table
    {
      sqlite3_stmt *sqlStatement;
      NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
      const char *sql = [[NSString stringWithFormat:@"pragma table_info('%s')",[table UTF8String]] UTF8String];
      if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
      {
        NSLog(@"Problem with prepare statement tableInfo %@",[NSString stringWithUTF8String:(const char *)sqlite3_errmsg(db)]);
    
      }
      while (sqlite3_step(sqlStatement)==SQLITE_ROW)
      {
        [result setObject:@"" forKey:[NSString stringWithUTF8String:(char*)sqlite3_column_text(sqlStatement, 1)]];
    
      }
    
      return result;
      }
    
    0 讨论(0)
  • 2020-11-27 09:46

    If you have the sqlite database, use the sqlite3 command line program and these commands:

    To list all the tables in the database:

    .tables
    

    To show the schema for a given tablename:

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