Retrieving value from database in Javascript
15,746
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
alert("onDeviceReady called");
}
function populateDB(tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (sname ,saddress ,sphone)');
var name=document.getElementById('n');
var address=document.getElementById('a');
var phone=document.getElementById('p');
tx.executeSql('INSERT INTO DEMO (sname ,saddress ,sphone) VALUES ('"+name.value+"','"+address.value+"','"+phone.value+"')');
}
function errorCB(tx, err)
{
alert("Error processing SQL: "+err);
}
// Transaction success callback
//
function successCB()
{
alert("success!");
}
function add()
{
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
for more see link here
//show data from db
// Transaction success callback
function show()
{
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(queryDB, errorCB);
}
// Query the database
function queryDB(tx)
{
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
// Query the success callback
function querySuccess(tx, results)
{
var len = results.rows.length;
console.log("DEMO table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " sname = " + results.rows.item(i).sname + " saddress = " + results.rows.item(i).saddress);
}
}
// Transaction error callback
function errorCB(err)
{
console.log("Error processing SQL: "+err.code);
}
Comments
-
prakash_d22 almost 2 years
I am new to mobile application development with PhoneGap. I have created a form to add and show name, address and phone number of a student in SQLite database. But the problem is I don't know to retrieve and display the values in the text boxes.
<!DOCTYPE HTML> <html> <head> <title>Contact Example</title> <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script> <script type="text/javascript" charset="utf-8"> document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000); db.transaction(populateDB, errorCB, successCB); } function populateDB(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (sname ,saddress ,sphone)'); } function errorCB(tx, err) { alert("Error processing SQL: "+err); } // Transaction success callback // function successCB() { alert("success!"); } function add(tx){ var name=document.getElementById('n'); var address=document.getElementById('a'); var phone=document.getElementById('p'); tx.executeSql('INSERT INTO DEMO (sname ,saddress ,sphone) VALUES ('"+name+"','"+address+"','"+phone+"')'); //tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")'); } function show(tx){ var name=document.getElementById('n'); tx.executeSql('SELECT * FROM DEMO WHERE (sname='"+name+"')'); document.f.n.value=name; document.f.a.value=//??; document.f.p.value=//??; } </script> </head> <body> <form name="f" method="get" action=""> Name :<input type="text" id="n" size="10"></input><br> Add :<input type="text" id="a" size="10"></input><br> Phone :<input type="text" id="p" size="10"></input><br> <input type="button" value="Add" onClick="add()"> <input type="button" value="Show" onClick="show()"> </form> </body> </html>