23

What is the best SQL for a SQLite database to effectively do:

If Database Table Exists then
  - create table
  - insert row
  - insert row (i.e. for startup data)
end
Michał Powaga
  • 21,562
  • 8
  • 48
  • 60
Greg
  • 33,110
  • 77
  • 244
  • 440

3 Answers3

28

To check that your table exists or not, you can use:

SELECT * FROM sqlite_master WHERE name ='myTable' and type='table'; 
Yaqub Ahmad
  • 27,316
  • 22
  • 100
  • 148
9

You can let Sqlite itself check this out for you:

CREATE TABLE IF NOT EXISTS <table_name> ...;    

Follow link for documentation: https://sqlite.org/lang_createtable.html

Nery Jr
  • 3,691
  • 1
  • 24
  • 24
5

Use this code

SELECT name FROM sqlite_master WHERE type='table' AND name='yourTableName';

if returning array count is equal to 1 its means table exist else not exist.

asmad
  • 387
  • 4
  • 13