iOS上的SQLite ... alter table IF?

时间:2015-12-04 17:13:45

标签: ios sqlite

我已经继承了现有的iOS应用程序代码库,并且被告知要更改sqlite表但不使用版本控制(因为不仅仅是在应用程序中的某个地方存储一个指示如何/是否更新表的常量)。有没有办法检查列是否存在,如果没有版本化就不存在该列?

1 个答案:

答案 0 :(得分:0)

使用SQLite的pragma table_info()获取每个表包含一行的结果集。例如:

create table foo (bar text, baz text)

pragma table_info('foo')

返回两行结果集:

[cid: 0, pk: 0, notnull: 0, name: bar, type: text, dflt_value: <null>]
[cid: 1, pk: 0, notnull: 0, name: baz, type: text, dflt_value: <null>]

因此,执行pragma table_info('table')然后遍历返回的行,看看是否存在name值等于您要查找的列的行。