2

How can I check weather specific table does exists or not in my database before execute query ?

Example : I want to check weather Detail table exists or not in InfoDB

I want to do some thing like :-

var createDB = Titanium.Database.open('InfoDB');
if(Detail exists in InfoDB)
  then
    var rs = createDB.execute('SELECT * FROM Detail');

Thanks...

Maulik
  • 19,348
  • 14
  • 82
  • 137
  • db.execute('CREATE TABLE IF NOT EXISTS you can do some thing like this to create the table, not a solution but a tip – Triode Feb 29 '12 at 13:40

3 Answers3

10

Try this:

var createDB = Titanium.Database.open('InfoDB');

var result = createDB.execute('SELECT name FROM sqlite_master WHERE type="table" AND name="your table name"');

if(result.isValidRow()) {
    //table found
   var rs = createDB.execute('SELECT * FROM Detail');
 }
 result.close();
Muhammad Zeeshan
  • 8,722
  • 10
  • 45
  • 55
2

Solved ! I use alternate way. I use try...catch instead.

var createDB = Titanium.Database.open('InfoDB');
try
{
   var rs = createDB.execute('SELECT * FROM Detail');
}
catch(err)
{
   alert(err)
}
Maulik
  • 19,348
  • 14
  • 82
  • 137
0

if you like to check that before creating table

db.execute('CREATE TABLE IF NOT EXISTS Detail (..columns..)')

or dropping table

db.execute('DROP TABLE IF EXISTS Detail')

avoids table exists or table not exists errors.

ilker
  • 190
  • 5
  • 11