Hi I have implemented a SqlLite in my project and i am creating the DB in Login.js, But My problem is Now i need to update a columns from Another JS. How to access a DB variable from another JS. Code A.js:
function onDeviceReady() {
window.db = window.openDatabase("SP_DB", "1.0", "SPDB", 200000);
};
Now i need to Access that window.db from another JS for columns add or update. How to achieve this
Code B.js: Here I need to access that window.db variable, i don't want to create DB again.
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);
}
Your variable window.db is in the global scope. So you can access it anywhere within the same page where it is defined. I hope the 'Another JS' that you are mentioning is in the same page. If it is in a different page it will not work.
Cordova applications should adopt the SPA (Single Page Application) design. If you do not use a SPA you need to define the required variables in each page. Please check https://cordova.apache.org/docs/en/latest/guide/next/#1-spa-is-your-friend
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With