I've attached $cordovaSQLite
to my ionic app. Here is a basic example of the code.
function retrieve() {
var q = $q.defer();
var query = 'SELECT user_credentials, user_id FROM Users;';
$ionicPlatform.ready(function (){
$cordovaSQLite.execute(db, query).then(function(res) {
if (res.rows.length > 0) {
console.log("found user");
console.log(res);
console.log(JSON.stringify(res));
q.resolve(res.rows[0]);
} else {
console.log("no rows found");
q.resolve(false);
}
}, function (err) {
q.reject(err);
});
});
return q.promise;
}
Here is the code to open up the db.
if(window.cordova) {
// App syntax
db = $cordovaSQLite.openDB( "CoolApp.db" );
} else {
// Ionic serve syntax
db = window.openDatabase("CoolApp.db", "1.0", "Cool App", -1);
}
When I test my app on Chrome, my logs show this
rows: SQLResultSetRowList
0: Object
user_credentials: "asdf"
user_id: 234
length: 1
rowsAffected: 0
However when I view the logs when running on my iOS app or Safari, I receive
{"rows":{"length":1},"rowsAffected":0,"insertId":1}
My question is why am I not receiving the value of rows
? Why does this work on the browser but not on iOS?
Did you try to instantiate a webSQL instead of SQLite DB for your browser?
You can always just go back to SQLite for your device, but modern browsers like Chrome and Firefox don't support SQLite.
You can get the results by querying the rows.item method with the corresponding index if multiple results were returned.
var elements = [];
for (var i = 0; i < result.rows.length; i++) {
elements.push(result.rows.item(i));
}
return elements;
Where result is the object returned by $cordovaSQL when its promise is complete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With