Search code examples
javascriptangularjssqlitecordovacordova-plugins

Cannot access cordova's SQL-lite storage plugin


I am building a simple app using Cordova and AnglerJS, for the database works I decided to use the cordova-sqlite-storage. Using Cordova's CLI command cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage.git I was able to install the plugin. But when I try to use it I get this error:

Uncaught TypeError: Cannot read property 'openDatabase' of undefined application.js:6

Here is the code:

document.addEventListener('deviceready', function(){
    var db = window.sqlitePlugin.openDatabase({name: "database.db", location: 2});
});

I am suspecting that the plugin itself/installation wasn't correct, are there any farther steps than using the cordova plugin add ... ?!


Solution

  • A Cordova/PhoneGap litehelpers/Cordova-sqlite-storage plugin open and use sqlite databases only on:

    • Android
    • iOS
    • Windows Universal(8.1) // Not Windows and Browser
    • Amazon Fire-OS
    • WP(7/8)

    with HTML5/Web SQL API.

    This plugin in Chrome or Firefox does not work.

    For example, for Android devices, if you have SQLite file "database.db" in your project folder [ mobileapp\www\ ] to connect database file just:

    var db = window.sqlitePlugin.openDatabase({name: "database.db", createFromLocation: 1});
    

    Some simple code:

    db.transaction(function(tx)
    {
        tx.executeSql('select * from TABLENAME;', [], function(tx, res)
        {
            console.log(res.rows);   
        }, 
        function(e){
            console.log("error: "+e.message);
        });
    }); 
    

    More informations: litehelpers/Cordova-sqlite-storage Docs