Creating database in Phonegap
Currently I am building a mobile app using Phonegap. In my project I need to store the data into the database and refresh it with server time to time.
Phonegap have the Storage API to do this. Storage API is based on the W3C HTML5 webdatabase. So we just need to create the database and corresponding table according to the webdatabase specification e.g;
[java]
var db = openDatabase("myApp", 1.0, "App database",200000)
[/java]
Now to create tables and insert data into it using this db instance
[java]
db.transaction(function(transaction){
transaction.executeSql("CREATE TABLE user (username Integer default NULL, UNIQUE(username));");
transaction.executeSql("INSERT INTO user (username) values (‘uday’);")
})
[/java]
Now to get the data from this table we will simply write the select query. The executeSql method has its own success and failure callback method. Success callback gives the result set from the query e.g;
[java]
db.transaction(function(transaction){
transaction.executeSql("SELECT * from user where username=?",[‘uday’],successCallback,function(e){
console.debug("some error")
})
})
function successCallback(transaction,results){
for (var i = 0; i < results.rows.length; i++) {
var username = results.rows.item(i).username;
// Some code
}
}
[/java]
The executeSql method takes four argument, first is the query, second array of values, if needed in the query, third success callback, which returns the result of query and last one error callback.
Can u please provide a sample for accessing prepopulated database using phonegap ??
I have tried other tutorials but no result..
Thanks in Advance..