Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite delete from only if the table exists

Right now I have this statement

DELETE FROM sqlite_sequence where name = 'table_name';

which works fine in most cases, but now I have a case where the sqlite_sequence table is not being created and so I get back

no such table: sqlite_sequence

I need a statement that only does the delete if the table exists. I've found this statement to tell me if the table exists or not

SELECT name FROM sqlite_master WHERE type='table';

But so far I've been unable to successfully pair it with the delete satement

like image 942
lightswitch05 Avatar asked Dec 05 '12 17:12

lightswitch05


1 Answers

The table existence check should include the name, of course

SELECT count(*) FROM sqlite_master WHERE type='table' AND name='table_name';

But you'll have to do it from the calling application in separate statements, i.e. run the above, and based on the count (naturally 1=true, 0=false) as boolean value, you can proceed to run the DELETE statement.

like image 90
RichardTheKiwi Avatar answered Oct 05 '22 11:10

RichardTheKiwi