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 entry suggests doing the select
SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'
and parse the result.
Is that the common way? Alternatives?
you can use Like statement if you are searching for any particular column
ex:
I know it is an old thread, but recently I needed the same and found a neat way:
Just for super noobs like me wondering how or what people meant by
You want to use use that as your prepare statement as shown below. Doing so selects a table that looks like this except is populated with values pertaining to your table.
Where id and name are the actual names of your columns. So to get that value you need to select column name by using:
Which will return the current row's column's name. To grab them all or find the one you want you need to iterate through all the rows. Simplest way to do so would be in the manner below.
An alternative way to get a list of column names not mentioned here is to select from a pragma function:
You can check if a certain column exists by running:
This is what you use if you don't want to parse the result of select sql from sqlite_master or pragma table_info.
Reference:
https://www.sqlite.org/pragma.html#pragfunc