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?
2 Answers
To detect if a particular table exists, use:
SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%your_table_name%' 2 Comments
SecretDeveloper
better answer as it checks the type.
finnw
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.
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);