tags:

views:

1909

answers:

4

How do I, reliably, check in SQLite, whether a particular user table exists?

I am not asking for unreliable ways like checking if a "select *" on the table returned an error or not ( is this even a good idea? ).

The reason is like this:

In my program, I need to create and then populate some tables if they do not exist already.

If they do already exist, I need to update some tables.

Should I take some other path instead to signal that the tables in question have already been created - say for example, by creating/putting/setting a certain flag in my program initialization/settings file on disc or something?

Or does my approach make sense?

+2  A: 

See this:

SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
Anton Gogolev
+5  A: 

You could try:

SELECT name FROM sqlite_master WHERE name='table_name'
Galwegian
+8  A: 

Missed that FAQ entry.

Anyways, for future reference, the complete query is:

SELECT name FROM sqlite_master WHERE type='table' AND name='table_name';
PoorLuzer
A: 

But it will throw SQLiteException if table doesn't exist so need to catch that.

Shrikant