i'我正在开发一个phonegap应用程序,但它无法连接数据库.我附加了以下程序

i'm developing a phonegap application but it cannot connect the database i attached the following program?

本文关键字:连接 数据库 程序 phonegap 开发 一个 应用程序      更新时间:2023-09-26

**我正在开发一个phonegap应用程序,但它无法连接我附加的数据库?我的代码如下:**

function onDeviceReady() {
 var mobno = document.getElementById("mobno").value;
 var cust = document.getElementById("cust").value;
 db = window.openDatabase("RegistrationDB", "1.0", "Registration", 200000);
 if (dbCreated)
 else
  db.transaction(populateDB, transaction_error, populateDB_success);
}
function populateDB(tx) {
 tx.executeSql('DROP TABLE IF EXISTS Registration');
 var sql = "CREATE TABLE IF NOT EXISTS Registration ( "
   + "mobno INTEGER(50), " + "custname VARCHAR(50))"; 
 tx.executeSql(sql);
 var mobno = document.getElementById("mobno").value;
 var cust =  document.getElementById("cust").value;
 tx.executeSql("INSERT INTO Registration (mobno,custname) VALUES ('"+ mobno +"','"+ cust +"')");
}
function transaction_error(tx, error) {
 alert("Database Error: " + error);
}
function populateDB_success() {
 dbCreated = true;
 // where you want to move
 alert("Successfully inserted");
  window.location="file:///android_asset/www/login.html";
}

如果你想使用Cordova SqlLite插件,你必须安装它:

cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin

接下来,您可以在设备中创建数据库:

function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
  db.transaction(function(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
    tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
      console.log("insertId: " + res.insertId + " -- probably 1");
      console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
      tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
        console.log("res.rows.length: " + res.rows.length + " -- should be 1");
        console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
      });
    }, function(e) {
      console.log("ERROR: " + e.message);
    });
  });
}