2

我是使用 PhoneGap 进行移动应用程序开发的新手。我创建了一个表单来在 SQLite 数据库中添加和显示学生的姓名、地址和电话号码。但问题是我不知道在文本框中检索和显示值。

<!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>
4

2 回答 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);
     }

有关更多信息,请参见此处的链接

//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);
    }
于 2012-06-22T05:45:22.767 回答
0

在您的代码中保存调用 db.transaction(populateDB, errorCB, successCB)

该函数将返回

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!");
}

对于检索,您可以使用以下内容:

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);
}
于 2015-10-19T14:43:40.840 回答