tags:

views:

38

answers:

3

I need my PHP app to be able to create an SQLite table but only if it doesn't already exist. How should I go about it?

+6  A: 

You can use:

CREATE TABLE IF NOT EXISTS <name> (
  /* definition */
)

Which is supported by SQLite (http://www.sqlite.org/syntaxdiagrams.html#create-table-stmt)

halfdan
Just tried it. I get: Warning: SQLiteDatabase::queryExec() [sqlitedatabase.queryexec]: near "NOT". It's a PHP app.
Emanuil
Can you post the code? Which SQLite version is this?
halfdan
The version is 3.6.20. Here's the code: $query = "CREATE TABLE IF NOT EXISTS messages (content TEXT, author TEXT)";$db->queryExec($query, $error) or die($error);
Emanuil
Works like a charm for me:sqlite> CREATE TABLE IF NOT EXISTS messages (content TEXT, author TEXT);sqlite> .tablesmessages
halfdan
Still get the warning. I'm going to use the `@` operator before the queryExec for the time being.
Emanuil
@Emanuil What's your SQLite version?
MPelletier
@MPelletier: The version is is 3.6.20.
Emanuil
+1  A: 

Use IF NOT EXISTS.

Space_C0wb0y
+1  A: 
CREATE TABLE IF NOT EXISTS ...
Drew Hall
The dots belong after the EXISTS :)
halfdan
@halfdan: Realized that right after I posted it. Your answer was so much more complete anyways... :)
Drew Hall