3

I'm creating a Sqlite DB & I have many pages that I want to access the db from.

If I have a new table that I want to create do I have to execute the entire db again ?

And If I have a statement like (Delete, insert, ... ).. Can I just access the db from any where within the code & call the "db.executesql()" whenever I need it?

Sana Joseph
  • 1,948
  • 7
  • 37
  • 58

1 Answers1

2

As PhoneGap's documentation at http://docs.phonegap.com/en/1.9.0/cordova_storage_storage.md.html#Storage says, you can do it this way:

// Cordova is ready
function onDeviceReady() {
    var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
    db.transaction(populateDB, errorCB, successCB);
}

// Populate the database 
function populateDB(tx) {
     tx.executeSql('DROP TABLE IF EXISTS DEMO');
     tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
     tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
     tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}

// Transaction error callback
function errorCB(tx, err) {
    alert("Error processing SQL: "+err);
}

// Transaction success callback
function successCB() {
    alert("success!");
}

So, you will need to open the database first, and then execute sql, like in the example:

var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);

For different purposes just create different functions with different sql execution code, just as populateDB above.

micadelli
  • 2,482
  • 6
  • 26
  • 38