I have inherited an existing iOS app codebase and have been told to alter a sqlite table but not use versioning (as in not just store a constant somewhere in the app that dictates how/whether to update the table). Is there a way to check whether a column exists and add that column if it doesn't exist without versioning?
Asked
Active
Viewed 143 times
1 Answers
0
Use SQLite's pragma table_info()
to get a result set that includes one row per table. For example:
create table foo (bar text, baz text)
and
pragma table_info('foo')
That returns a two row result set:
[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>]
So, perform pragma table_info('table')
and then iterate through the rows returned and see if a row with a name
value equal to the column you're looking for exists.

Rob
- 415,655
- 72
- 787
- 1,044