Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Access a Sql lite Variable From one Js to another Js in Cordova

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);
    }
like image 758
Piku Avatar asked Sep 28 '16 12:09

Piku


1 Answers

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

like image 195
Samuel T Thomas Avatar answered Oct 28 '22 14:10

Samuel T Thomas