candlejack
candlejack

Reputation: 1209

Check if exist database in Cordova/Phonegap

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!

Upvotes: 2

Views: 3190

Answers (1)

AtanuCSE
AtanuCSE

Reputation: 8940

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.

Upvotes: 3

Related Questions