22

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?

Emanuil Rusev
  • 33,269
  • 52
  • 129
  • 197
  • 2
    Possible 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
  • 1
    you 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 Mar 26 '19 at 12:45

3 Answers3

45

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
  • 32,443
  • 8
  • 76
  • 85
6
CREATE TABLE IF NOT EXISTS ...
Drew Hall
  • 27,679
  • 12
  • 59
  • 81
5

Use IF NOT EXISTS.

Björn Pollex
  • 72,744
  • 28
  • 189
  • 274