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
  • 34,563
  • 55
  • 137
  • 201
  • 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 S M Mar 26 '19 at 12:45

3 Answers3

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
6
CREATE TABLE IF NOT EXISTS ...
Drew Hall
  • 28,429
  • 12
  • 61
  • 81
5

Use IF NOT EXISTS.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283