2

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 &nbsp;&nbsp;&nbsp; :<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>

2 Answers 2

2
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);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

thanks @Tamkeen .can u help me for adding data also with function add().
@Prakash recommend you to see the link first.
see it will give u id not the value of text box ie document.getElementById('n') u r trying to get the id not value for the vlale need to do that.
i have given the link to you see that. it will give all info to make the all (select ,del,update) operation.
0

Inside your code for save call db.transaction(populateDB, errorCB, successCB)

The function will return to

function populateDB(tx) {
var rr=escape(JSON.stringify(onedata));//here onedata is data you want to save I use json data here.
tx.executeSql('CREATE TABLE IF NOT EXISTS LeadInfo (data)');
tx.executeSql('INSERT INTO LeadInfo (data) VALUES ("'+ rr +'")');
alert("Insert")
}

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

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

For retrieval you can use following:

function queryDB(tx) {
tx.executeSql('SELECT * FROM LeadInfo', [], querySuccess, errorCB);
}

function querySuccess(tx, results) {
var tablereport="";
if (results != null && results.rows != null) {
for (var i = 0; i < results.rows.length; i++) {
var row =unescape(results.rows.item(i).data);
var obj = JSON.parse(row);
tablereport+='<a href="#"><span  style="font-size:18px; font-weight:400;  padding:10px 0px 10px 0px;">'+JSON.stringify(obj.lead_name)+'</span><span style="padding: 10px 16px;width: 100px;float: right;margin-top: -33px;margin-right: -80px;font-size:14px"></span><br>';
tablereport+='</li>';
tablereport+='</li>';
}
}
}



function errorCB(err) {
alert("Error processing SQL Retrive: "+err.code);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.