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?
Asked
Active
Viewed 2.0k times
22
-
2Possible duplicate of [Create table in SQLite only if it doesn't exist already](https://stackoverflow.com/questions/4098008/create-table-in-sqlite-only-if-it-doesnt-exist-already) – Tas Jun 08 '17 at 10:45
-
1you can do as shown ;) [here](https://i.stack.imgur.com/8HyfK.png) ;use the sql statement CREATE TABLE IF NOT EXISTS and then specify column name their types – Imran S M Mar 26 '19 at 12:45
3 Answers
46
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
- 33,545
- 8
- 78
- 87
-
1Just tried it. I get: Warning: SQLiteDatabase::queryExec() [sqlitedatabase.queryexec]: near "NOT". It's a PHP app. – Emanuil Rusev Sep 15 '10 at 09:48
-
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 Rusev Sep 15 '10 at 10:00
-
Works like a charm for me: sqlite> CREATE TABLE IF NOT EXISTS messages (content TEXT, author TEXT); sqlite> .tables messages – halfdan Sep 15 '10 at 10:05
-
Still get the warning. I'm going to use the `@` operator before the queryExec for the time being. – Emanuil Rusev Sep 15 '10 at 11:40
-
-