如何获取sqlite3 / iPhone上的列名称列表?
我想将我的iPhone应用程序迁移到新的数据库版本。 由于我没有保存一些版本,我需要检查是否存在某些列名。
这个Stackoverflow条目建议做选择
SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'
并解析结果。
这是常用的方式吗? 备择方案?
PRAGMA table_info(table_name);
会为您提供所有列名称的列表。
如果你有sqlite数据库,使用sqlite3命令行程序和这些命令:
列出数据库中的所有表格:
.tables
  要显示给定tablename的模式: 
.schema tablename
只为像我这样的超级新手想知道如何或者什么人的意思
PRAGMA table_info('table_name') 
你想使用这个作为你的准备陈述,如下所示。 这样做会选择一个看起来像这样的表格,除了填充与表格有关的值。
cid         name        type        notnull     dflt_value  pk        
----------  ----------  ----------  ----------  ----------  ----------
0           id          integer     99                      1         
1           name                    0                       0
其中id和name是您列的实际名称。 因此,要获得该值,您需要通过使用以下选项来选择列名称:
//returns the name
sqlite3_column_text(stmt, 1);
//returns the type
sqlite3_column_text(stmt, 2);
这将返回当前行的列名称。 要全部抓住它们或找到你想要的,你需要遍历所有行。 最简单的方式就是以下面的方式。
//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
    }
}
