javascript - SQLite plugin Cordova basic code -
i'm new cordova & sqlite wrote code , can't figure out wrong it? suggestions? following output javascript debugger:
<script type="text/javascript"> // wait cordova load document.addeventlistener('deviceready', ondeviceready, false); var output = document.getelementbyid('outputfield'); // cordova 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 ' + table + ' (key,value) values ("' + key + '","' + value + '")'); }); } function readvalue(db, table, key) { db.transaction(function (tx) { return db.executesql(tx, 'select * ' + table + ' key="' + key + '"'); }); } </script>
if testing new plugin, library, … whatever, best way read docs, play little bit examples , expand step step needs.
the sqlite plugin event driven, means, have wait until job done.
you doing way , don't work:
var the_result = mysql_job(); function mysql_job(){ db.readtransaction(function(tx) { return db.executesql(…); }); }
the right way is:
mysql_job(); function mysql_job(some_values){ tx.executesql("select * mytable", [], function(tx, res) { //here goes result handling, inserting values in html }, function(error) { console.log('sqlite error: ' + error.message); }); }
this have every sql job, please see docs at: https://github.com/litehelpers/cordova-sqlite-storage
if have lot of queries idea use promises: how compact sql instructions in cordova?
Comments
Post a Comment