24

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
  • 22,561
  • 8
  • 51
  • 62
Greg
  • 34,042
  • 79
  • 253
  • 454

3 Answers3

32

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

SELECT * FROM sqlite_master WHERE name ='myTable' and type='table'; 
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
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,849
  • 1
  • 26
  • 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