电话号码插入查询未发生

时间:2015-06-04 07:45:14

标签: cordova

在phonegap我要插入值, 我正在调用onclick函数但查询没有插入

function AddNewRecord(tx)
{
    var RollNo = $('#txtRollNo').val();
    var Fname = $('#txtFirstName').val();
    var Lname = $('#txtLastName').val();
    var MobNo = $('#txtMobNo').val();

    var sql = 'INSERT INTO Test(ROLLNO, FirstName, LastName, MobileNo) VALUES ("'+RollNo+'" , "'+Fname+'" , "'+Lname+'" , "'+MobNo+'")';
    alert(sql);
    tx.executeSql(sql, querySuccess, errorQuery);
}

这里是phonogap中的body onclick函数

<body > 
    <input id="txtRollNo" type="text" placeholder="Roll No">
    <input id="txtFirstName" type="text" placeholder="FirstName"> 
    <input id="txtLastName" type="text" placeholder="Last Name"> 
    <input id="txtMobNo" type="text" placeholder="Mob No">

    <input type="button" value="Add New" onClick="AddNewRecord();"> 
</body>

1 个答案:

答案 0 :(得分:0)

var mydb;


function createAndInitDatabase() {
 mydb = window.openDatabase("dbname", "0.1", "Database for My App", 1024*1024);
//Create the database the parameters are 1. the database name 2.version number 3. a description 4. the size of the database (in bytes) 1024 x 1024 = 1MB x 1024 = 1 GB

    // create the users table using SQL for the database using a transaction
    mydb.transaction(function(t) {                      
         t.executeSql("CREATE TABLE IF NOT EXISTS Test (id INTEGER PRIMARY KEY ASC, rollno TEXT,firstname TEXT, lastname TEXT, mobileno TEXT)");
    });
}

//Insert function
function insertData(){
       var RollNo = $('#txtRollNo').val();
       var Fname = $('#txtFirstName').val();
       var Lname =$('#txtLastName').val();
       var MobNo = $('#txtMobNo').val();
    if (mydb) {

        mydb.transaction(function(t) {
            t.executeSql("INSERT INTO Test (rollno, firstname,lastname,mobileno) VALUES (?,?,?,?)", [RollNo,Fname,Lname,MobNo],insertSuccessCallback,errorCallBack);
        });
        function insertSuccessCallback() {
          console.log('in insert success callback');
        };
        function errorCallBack() {
          console.log('in insert errorCallBack');
        };

    } else {
        console.log('db is not available');
    }

}