How do I use sqlite with cordova while testing in a normal desktop browser? How do I use sqlite with cordova while testing in a normal desktop browser? angularjs angularjs

How do I use sqlite with cordova while testing in a normal desktop browser?


You can use a simple wrapper which uses WebSQL for the browser and SQLite for the device. Just use different Database object. WebSQL and SQLite API is close to identical. You only need different initialization for browser and device.

Here is my example code(ES6):

var runiOS = false;var DB;// Check browser or devicevar onDeviceReady = new Promise(function(resolve, reject) {        if (document.URL.match(/^https?:/i)) {        console.log("Running in a browser...");        resolve();    } else {        console.log("Running in an app...");        runiOS = true;        document.addEventListener("deviceready", resolve, false);    }});// Run applicationonDeviceReady.then(function() {    //  Init WebSQL on browser or SQLite on device    if (runiOS) {        DB = window.sqlitePlugin.openDatabase({ name: 'my.db', location: 'default' }, function (db) {}, function (error) { console.log('Open database ERROR: ' + JSON.stringify(error)); });                  console.log('DB: SQLite');    }    else {        DB = window.openDatabase('my', "0.1", "My list", 200000);            console.log('DB: WebSQL');    }    // ...    db.transaction(function(tx) {        tx.executeSql('CREATE TABLE IF NOT EXISTS DemoTable (name, score)');        tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Alice', 101]);        tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Betty', 202]);    }, function(error) {        console.log('Transaction ERROR: ' + error.message);    }, function() {        console.log('Populated database OK');    });    db.transaction(function(tx) {        tx.executeSql('SELECT count(*) AS mycount FROM DemoTable', [], function(tx, rs) {            console.log('Record count (expected to be 2): ' + rs.rows.item(0).mycount);        }, function(tx, error) {            console.log('SELECT error: ' + error.message);        });    });});


Here is the solution that works for me based on the answer of Дмитрий Васильев

if (navigator.userAgent.match(/(Android)/)) {    db = window.sqlitePlugin.openDatabase({name: 'mydb.db', location: 'default' }, function (db) {}, function (error) { console.log('Open database ERROR: ' + JSON.stringify(error)); });              console.log('DB: SQLite');}else {    db = window.openDatabase('mydb', "0.1", "mydb description", 200000);        console.log('DB: WebSQL');}

What happen here is that if the Android device is detected, the db object will switch to SQLITE or else if it is the browser, it will use WebSql.

To detect more devices you could use this:

if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {    db = window.sqlitePlugin.openDatabase({name: 'mydb.db', location: 'default' }, function (db) {}, function (error) { console.log('Open database ERROR: ' + JSON.stringify(error)); });              console.log('DB: SQLite');}else {    db = window.openDatabase('mydb', "0.1", "mydb description", 200000);        console.log('DB: WebSQL');}

All you have to do is then run your code as normal and the db object will use the right database type.