ngCordova - Cannot get SQLite records

1.1k views Asked by At

I'm wondering what i'm doing wrong here, it's hell to debug it like most of cordova plugins...

I'm simply trying to store a few strings... (I only need it in one controller)

var db = $cordovaSQLite.openDB({ name: "videos" });
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS videos (id integer primary key, video mediumtext)");

// getting some data from the server, nothing special

query = "INSERT INTO videos (id, video) VALUES (?)";
$cordovaSQLite.execute(db, query, [response.Data.media]).then(function(result) {
  console.log(result);
  var message = "INSERT ID -> " + result.insertId;
  console.log(message);
}, function (err) {
  console.error(err);
  //alert(err);
});

I get the inserted ID, so it must be saving something... But when I try to load it using a function

var query = "SELECT * FROM videos where id = 1";
$cordovaSQLite.execute(db, query).then(function(result) {
  console.log(result);
});

I get nothing.

enter image description here

Astrophysicists would call it empty space, but we are developers and we need data to process! Why is there nothing in rows? (no matter if I select where id or everything, it is just showing me this kind of empty object with no data).

Much thanks for any help!

1

There are 1 answers

0
area28 On BEST ANSWER

You should be able to loop over the result like this:

var output = [];
for (var i = 0; i < result.rows.length; i++) {
    output.push(result.rows.item(i));
}
console.log(JSON.stringify(output));

Then when you look at output it will contain an array of results with data.