I can not insert data in a SQLite database

Hi everybody, I am not able to insert a record in a SQLite db. I can read a record but I can not insert. This is the code:

db = $cordovaSQLite.openDB({name: "name.db", location: 1});
service.validateLogin($scope.fields.email, $scope.fields.password)
            .success(function (data, status, headers, config) {
                if (data.esito == 'ok') {
                    user.login(data);

                    db.transaction(function(tx) {
                        tx.executeSql("INSERT INTO table (id, email) VALUES (?,?)", [data.id, data.email], function(tx, res) {
                            alert('INSERTED');
                        }, function(e) {
                            alert('ERROR: ' + e.message);
                        });
                    });

The problem is that I do not get any alert. It does not enter in the executeSql function.
I tried with this code, with same result:

var query = "INSERT INTO table (id, email) VALUES (?,?)";
$cordovaSQLite.execute(db, query, [data.id, data.email]).then(function(res) {
    alert('INSERTED')
}, function (err) {
    alert('ERROR');
});

Any helps? Thanks in advance.

What error messages show up in your logs?

I thank you for your reply, but you did not read my question entirely :smile:

I don’t get any message, thats my problem, neither of success nor error…:((((

I put alerts for that.

Sorry for the confusion. I was not asking for the callback error message. The callback success and error messages will only happen if the command succeeded in some fashion. I was asking for the exception message which would only appear in your logs when the command crashes.

Alerts won’t help you for the exception message. You need to look at your logs.

I don’t get any message in log.

I noticed that in Android 4.4.2 it works, in Android 5 does not works…:frowning:

Just to clarify, you’re looking at the ADB logs, correct?

Yes, I put just before and just after the code 2 logs, and they appear in logcat. But nothing else.

Can you show how you’re creating your table? Maybe your table is not correct.

Also, I think the openDB command has been changed to no longer take an object. You might want to double check that.

Two possible approaches you could try:

  1. Strip out your validateLogin step. This may be the cause of your error.

  2. Put in a fallback to webSQL to check that the code is working at all. You can inspect your webSQL DB in Chrome with inspect. Like this:

     if($cordovaSQLite && $window.sqlitePlugin !== undefined){
       db = $cordovaSQLite.openDB({ name: dbName });
       $window.alert("SQLite plugin detected");
     }
     else {
      db = $window.openDatabase( dbName, '1.0', 'stats db', 200000);
      console.log("WebSQL db is called", dbName);
     } 
    

I say this as someone who has code VERY similar to yours, but I have a working DB.

This is how I create the db (in a module):

.run(function($ionicPlatform, $cordovaSQLite) {
    $ionicPlatform.ready(function() {
        if (window.cordova) {
            db = $cordovaSQLite.openDB({name: "name.db", location: 1}); //device
        }else{
            db = window.openDatabase("name", "1.0", "name", 1024 * 1024 * 100); // browser
        }
        $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS user (id integer primary key , email text, token text)");
    });
})

I always get WebSQL db is called, either in browser or in emulator or device.

So you know its an issue with ngCordova.

Are you sure that if statement works? Have you tried

if(typeof window.cordova != "undefined")

?

I solved it!!

The problem was after the call. I called a redirect, and I forgot (I didn’t know actually) that the insert was done asynchronously…:frowning:

I just call the redirect in “then” function, so I am sure it called after the insert.

Thanks for your help and sorry for wasting your time.

ciao giacomo, sono un entry level e mi hanno dato da fare uno slidemenu con log in e db…ma non riesco a fare il db e a collegarci il log in…mica puoi darmi una mano??ovviamente in ionic.Grazie in anticipo

A better code with a Model service: https://github.com/jdnichollsc/Ionic-Starter-Template

Regards, Nicholls