2

How can I determine in a table already exists before I start adding data to it again, in as few lines of code as possible?

Jules
  • 7,568
  • 14
  • 102
  • 186

2 Answers2

4

If you are using sqlite directly, you can use the following query to see if the table already exists:

SELECT name FROM sqlite_master WHERE type='table' AND name='my_table_name';
Codebeef
  • 43,508
  • 23
  • 86
  • 119
4
sqlite3_stmt *statementChk;
sqlite3_prepare_v2(database, "SELECT name FROM sqlite_master WHERE type='table' AND name='util_nums';", -1, &statementChk, nil);

bool boo = FALSE;

if (sqlite3_step(statementChk) == SQLITE_ROW) {
    boo = TRUE;
}
sqlite3_finalize(statementChkUN);
Jules
  • 7,568
  • 14
  • 102
  • 186