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
2010-09-15 09:45:39
Just tried it. I get: Warning: SQLiteDatabase::queryExec() [sqlitedatabase.queryexec]: near "NOT". It's a PHP app.
Emanuil
2010-09-15 09:48:29
Can you post the code? Which SQLite version is this?
halfdan
2010-09-15 09:57:25
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
2010-09-15 10:00:02
Works like a charm for me:sqlite> CREATE TABLE IF NOT EXISTS messages (content TEXT, author TEXT);sqlite> .tablesmessages
halfdan
2010-09-15 10:05:00
Still get the warning. I'm going to use the `@` operator before the queryExec for the time being.
Emanuil
2010-09-15 11:40:36
@Emanuil What's your SQLite version?
MPelletier
2010-09-15 12:58:29
@MPelletier: The version is is 3.6.20.
Emanuil
2010-09-15 15:53:28