2

I'm developing a Cordova/Phonegap application, Basicaly I want to know: how I can check if database exist?

Before accessing it, to show a message and avoid an SQL error. Thank you!

candlejack
  • 1,189
  • 2
  • 22
  • 51
  • I've answered a similar question: [Phonegap check if database exists](http://stackoverflow.com/questions/22125706/sqlite-check-if-database-exist/33674394#33674394) – Lanceomagnifico Nov 12 '15 at 15:02

1 Answers1

3

If you're using webSQL,then

var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);

Calling this line means if a database named 'mydb' exists, then it will open it and if doesn't exist it'll create one.

openDatabase: This method creates the database object either using existing database or creating new one.

See here

And to make sure that you don't call a not existing table you can use this line in your device ready

var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {  
   tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
});

It will create a table with your desired name if not exists. AFAIK this is the way.

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112