您好我已经在我的项目中实现了一个SqlLite,我在Login.js中创建了数据库,但我的问题是现在我需要更新来自Another JS的列。 如何从另一个JS访问DB变量。 代码A.js:
function onDeviceReady() {
window.db = window.openDatabase("SP_DB", "1.0", "SPDB", 200000);
};
现在我需要从另一个JS访问 window.db 以获取列添加或更新。如何实现这个
Code B.js: 在这里,我需要访问 window.db 变量,我不想再次创建数据库。
var saveimg = document.getElementById("saveimg");
saveimg.addEventListener('click', goInsert, false);
function goInsert() {
window.db.transaction(insertDB, errorCB, successCB);
}
function insertDB(tx) {
tx.executeSql('INSERT INTO SP(FirstName,LastName,Address) VALUES ("' + document.getElementById("txtFirstName").value + '","'
+ document.getElementById("txtLastName").value + '","' + document.getElementById("txtAddress").value + '")');
}
function errorCB(err) {
alert("Error processing SQL: " + err.code);
}
function successCB() {
alert("success!");
db.transaction(queryDB, errorCB);
}
function queryDB(tx) {
tx.executeSql('SELECT * FROM SP', [], querySuccess, errorCB);
}
答案 0 :(得分:2)
您的变量window.db位于全局范围内。因此,您可以在定义它的同一页面中的任何位置访问它。 我希望你提到的'Another JS'在同一页面。如果它在不同的页面中则不起作用。
Cordova应用程序应采用SPA(单页应用程序)设计。如果您不使用SPA,则需要在每个页面中定义所需的变量。请检查https://cordova.apache.org/docs/en/latest/guide/next/#1-spa-is-your-friend