vendredi 5 février 2016

SQLite plugin Cordova basic code

I'm new to Cordova & Sqlite but I wrote some code and I can't figure out what is wrong with it? Any suggestions? I always get the following output from the Javascript debugger:

Click to see error messages

 <script type="text/javascript">
    // Wait for Cordova to load
    document.addEventListener('deviceready', onDeviceReady, false);
    var output = document.getElementById('outputField');

    // Cordova is ready
    function onDeviceReady() {
        window.sqlitePlugin.openDatabase({ name: 'test.db', location: 2 }, function (db) {
            output.innerHTML += '</br> - Database created/opened';

            db.transaction(function (tx) {
                tx.executeSql(tx, "CREATE TABLE  localStorage2 IF NOT EXISTS (key UNIQUE, value)");
            });


            output.innerHTML += '</br> - Table localStorage2 Created';

            storeValue(db, 'localStorage2', 'testKey', 'testValue');
            output.innerHTML += '</br> - Insert dummy value';

            output.innerHTML += '</br> ' + readValue(db, 'localStorage2', 'testKey');
        });
    }


    function storeValue(db, table, key, value) {
        db.transaction(function (tx) {
            tx.executeSql(tx, 'INSERT INTO ' + table + ' (key,value) VALUES ("' + key + '","' + value + '")');
        });

    }

    function readValue(db, table, key) {
        db.transaction(function (tx) {
            return db.executeSql(tx, 'SELECT * FROM ' + table + ' WHERE key="' + key + '"');
        });
    }
</script>

Aucun commentaire:

Enregistrer un commentaire