Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a table exists in sqlite3 c++ API? [duplicate]

Tags:

c++

sqlite

I'm opening a database file and potentially creating it if it doesn't exist.

But for some reason, this doesn't create the table. Any ideas?

const char* sql = "CREATE TABLE IF NOT EXISTS blocks(id text primary_key,length numeric)";

sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db_, create_table_sql, -1, &stmt, NULL);
rc = sqlite3_step(stmt);

I haven't got it in here by yes I'm checking the return code at each point. There are no errors.

Perhaps there is a better way to accomplish this?

like image 576
hookenz Avatar asked Aug 17 '10 04:08

hookenz


People also ask

How do I drop a table in SQLite?

To drop a table in SQLite, use the DROP TABLE statement. Running this statement removes the table from the database. It is completely removed from the database schema and the disk file. Therefore the table can not be recovered.


1 Answers

Execute the following SQL:

select 1 from sqlite_master where type='table' and name='TABLE_NAME_TO_CHECK'

If you get a row then the table exists. If the result set is empty then it doesn't.

like image 96
Anthony Williams Avatar answered Oct 07 '22 09:10

Anthony Williams