0

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
helloB
  • 3,472
  • 10
  • 40
  • 87

1 Answers1

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