cordovaSQLite not working on device

App working with

db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100);

but not with

 db = $cordovaSQLite.openDB({ name: "my.db" })

on the device and this is how I have created table and inserted data into it

 $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS clock (timeid integer primary key, clocking text,latitude text,longitude text, accuracy text)");

 query = "INSERT INTO clock(clocking, latitude, longitude, accuracy) VALUES(?,?,?,?)";
                $cordovaSQLite.execute(db,query,[clocking,latitude,longitude,accuracy]);

Where could I be going wrong?

Are you receiving any errors?

@Gajotres Tested in Chrome Developer console … no errors reported

One thing to check is be sure that you are opening the DB in or after deviceready has fired.

If you are doing it before deviceready the window.openDatabase will succeed but the $cordovaSQLite.openDB will likely fail.

Also, sometimes deviceready fires after the various view show / before show events so can’t rely on those to occur after deviceready.

@bmwilson74 I have been careful regarding that

angular.module('ionicApp', ['ngCordova', 'ionic'])
       .run(function ($ionicPlatform, $cordovaSQLite) {
            $ionicPlatform.ready(function () {
                if (window.cordova && window.cordova.plugins.Keyboard) {
                    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                }
                if (window.StatusBar) {
                    StatusBar.styleDefault();
                }  
                db = $cordovaSQLite.openDB({ name: "my.db" })
                $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS clock (timeid integer primary key, clocking text,latitude text,longitude text, accuracy text)");
            });
        })

Any Idea what could I be doing stupid here

I’m grasping at straws here but maybe try:

var db = window.sqlitePlugin.openDatabase({name: “my.db”});

…bypassing the Angular wrapper and see if it makes a difference.

Hook up the Chrome debugger (http://www.raymondcamden.com/2014/01/02/Apache-Cordova-33-and-Remote-Debugging-for-Android) and post the JavaScript console to see if there are any errors.

thanks @bmwilson74 for the help but today I finally figured out where am I going wrong. I was following this tutorial by Nic Raboy


Since I have also declared by database globally so my controllers are ready to access the database but what I didn’t realize is that you still need to make the onDeviceReady() method every time so in his tutorial the Insert and Select statement should be more like this

example.controller("ExampleController", function ($scope, $cordovaSQLite, $ionicPlatform) {

    $scope.insert = function (firstname, lastname) {
        var query = "INSERT INTO people (firstname, lastname) VALUES (?,?)";
        $ionicPlatform.ready(function () {
            $cordovaSQLite.execute(db, query, [firstname, lastname]).then(function (res) {
                console.log("INSERT ID -> " + res.insertId);
            }, function (err) {
                console.error(err);
            });
        });
    }

    $scope.select = function (lastname) {
        var query = "SELECT firstname, lastname FROM people WHERE lastname = ?";
        $ionicPlatform.ready(function () {
            $cordovaSQLite.execute(db, query, [lastname]).then(function (res) {
                if (res.rows.length > 0) {
                    console.log("SELECTED -> " + res.rows.item(0).firstname + " " + res.rows.item(0).lastname);
                } else {
                    console.log("No results found");
                }
            }, function (err) {
                console.error(err);
            });
        });
    }

});

Correct me if I am wrong

Why don’t you wrap all of your code into $ionicPlatform.ready?

Its not working without it.

I know it’s not working without it. I said, and I will repeat it, wrap all of your code inside one (signle) $ionicPlatform.ready, no point in repeating it more then once.

Oh yeah sorry for that…that’s just to explain the concept…

@Gajotres I would like to know by this approach am I using indexeddb or WebSQL?

@vardhan, concerning the deviceready, you can by pass it with this trick :

var callback = function () {
    angular.bootstrap(document, ['app']);
};
if (window.cordova) {
    document.addEventListener('deviceready', callback, false);
}
else {
    window.addEventListener('load', callback, false);
}

By the way, maybe this module would be helpful : https://github.com/abellion/ngDatabase

Glad it’s working. Makes sense.

I’ve not had to wrap things up like that because I either do db operations in response to a user event (like a button click) or if I need to access the db as soon as the first controller loads when the App starts then I set a flag in ready() and wait for that flag to become set in the controller before doing anything.

Try to update the plugin cordova-sqlite storage to version 0.7.14
That works for me.