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);
});
});
}
});
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.
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.