**我正在开发一个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";
}
答案 0 :(得分:1)
如果您想使用Cordova Sqlite插件,则必须安装它:
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);
});
});
}