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
I know it's too late but this will help other.
To find the column name of the table, you should execute select * from tbl_name
and you will get the result in sqlite3_stmt *
. and check the column iterate over the total fetched column. Please refer following code for the same.
// sqlite3_stmt *statement ;
int totalColumn = sqlite3_column_count(statement);
for (int iterator = 0; iterator<totalColumn; iterator++) {
NSLog(@"%s", sqlite3_column_name(statement, iterator));
}
This will print all the column names of the result set.
PRAGMA table_info(table_name);
will get you a list of all the column names.
When you run the sqlite3
cli, typing in:
sqlite3 -header
will also give the desired result
Maybe you just want to print the table headers on the console. This is my code: (for each table)
// ------------------ show header ----------------
char sqlite_stmt_showHeader[1000];
snprintf(sqlite_stmt_showHeader, 1000, "%s%s", "SELECT * FROM ", TABLE_NAME_STRING UTF8String]);
sqlite3_stmt* statement_showHeader;
sqlite3_prepare_v2(DATABASE, sqlite_stmt_showHeader, -1, &statement_showHeader, NULL);
int headerColumnSize = sqlite3_column_count(statement_showHeader);
NSString* headerRow = @"|";
for (int j = 0; j < headerColumnSize; j++) {
NSString* headerColumnContent = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_name(statement_showHeader, j)];
headerRow = [[NSString alloc] initWithFormat:@"%@ %@ |", headerRow, headerColumnContent];
}
NSLog(@"%@", headerRow);
sqlite3_finalize(statement_showHeader);
// ---------------- show header end ---------------------
Just for super noobs like me wondering how or what people meant by
PRAGMA table_info('table_name')
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.
cid name type notnull dflt_value pk
---------- ---------- ---------- ---------- ---------- ----------
0 id integer 99 1
1 name 0 0
Where id and name are the actual names of your columns. So to get that value you need to select column name by using:
//returns the name
sqlite3_column_text(stmt, 1);
//returns the type
sqlite3_column_text(stmt, 2);
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.
//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
}
}
.schema table_name
This will list down the column names of the table from the database.
Hope this will help!!!