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?
            Asked
            
        
        
            Active
            
        
            Viewed 3,280 times
        
    2 Answers
7
            To detect if a particular table exists, use:
SELECT name 
  FROM sqlite_master
 WHERE type = 'table'
   AND name LIKE '%your_table_name%'
 
    
    
        OMG Ponies
        
- 325,700
- 82
- 523
- 502
- 
                    better answer as it checks the type. – SecretDeveloper Sep 19 '09 at 21: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 Sep 21 '09 at 09:56
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); 
    
    
        David Crawshaw
        
- 10,427
- 6
- 37
- 39
