tags:

views:

38

answers:

3

I have a parser that parses XML file into SQLite database, and the current implementation generates the 'create table xyz ...' even the table is already existed.

  • Is this OK? I mean, is this OK to run 'create table' even when the table exists in db?
  • If not, is there easy way to check the names of tables (and its contents) that SQLite db has?
+4  A: 

What you are searching for is CREATE TABLE IF NOT EXISTS and the FAQ entry How do I list all tables/indices contained in an SQLite database.

Creating a table without the "IF NOT EXISTS" option will lead to an error.

AndiDog
+1  A: 

You can DROP TABLE before CREATE TABLE, you can safety DROP TABLE who don't exists then you don't must checking for TABLE existence before DROP.

Svisstack
DROP TABLE can be combined with the 'IF EXISTS' clause as well to prevent an error.
bot403
+1  A: 

SQLite has a "IF NOT EXISTS" clause so that you can throw a "CREATE TABLE" at the database and it will ignore it if it already exists. For example:

CREATE TABLE IF NOT EXISTS mytable ( id INTEGER );

The URL to the documentation about this is at: http://www.sqlite.org/lang_createtable.html

Sean Reifschneider