Is there a way to test for the existence of a table in a SQLite database? Right now, I'm creating the table inside a try catch block, and if that throws an exception I know the table has been created. Surely there has to be a better way, right?
+4
A:
There is a table called sqlite_master
that contains the database schema. You can run a query like:
select count(*) from sqlite_master where name='users';
If the query returns 1, the table 'users' exists. You can also use the if not exists
SQL construction:
create table if not exists users (name, pwd);
David Crawshaw
2009-09-19 20:58:11
+4
A:
To detect if a particular table exists, use:
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name LIKE '%your_table_name%'
OMG Ponies
2009-09-19 21:05:55
better answer as it checks the type.
Kaius
2009-09-19 21:09:09
I don't think there is any advantage in checking the type. If there is an existing index or trigger with the same name, it will still fail to create the table.
finnw
2009-09-21 09:56:10