0

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?

0

2 Answers 2

7

To detect if a particular table exists, use:

SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%your_table_name%' 
Sign up to request clarification or add additional context in comments.

2 Comments

better answer as it checks the type.
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.
4

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.