Precompiled SQLight DB - Cannot read property 'transaction' of null

I’m following this guide:

I get this problem:

error TypeError: Cannot read property 'transaction' of null
at Object.execute (http://192.168.0.10:8100/js/ng-cordova.min.js:9:19174)

I think it is due to the fact that home controller is launched before that copy is finished.

This is my code:

app.run(function($ionicPlatform, $cordovaSQLite) {
    
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }

        window.plugins.sqlDB.copy("comeInComoDB.db", function() {
            console.log('db copy completed');
            db = $cordovaSQLite.openDB("comeInComoDB.db");
        }, function(error) {
            console.error("There was an error copying the database: " + JSON.stringify(error));
            db = $cordovaSQLite.openDB("comeInComoDB.db");
        });
        
        

    });
});

My HomeCtrl

  app.controller('HomeCtrl', function($scope, $rootScope, $cordovaLaunchNavigator, $cordovaSQLite) {

  var query = "SELECT name FROM monument";
              
  $cordovaSQLite.execute(db, query, []).then(function(res) {
      if(res.rows.length > 0) {
          for(var i = 0; i < res.rows.length; i++) {
              console.log("SELECTED -> " + res.rows.item(i).name);
          }
      } else {
          console.log("No results found");
      }
  }, function (err) {
      console.error('Error executing Query!');
      console.error(JSON.stringify(err));
  });

I also have seen that commenting query code on HomeCtrl I don’t get any error, but also I see that console.log(‘db copy completed’);

I think that my copy doesn’t complete and remains appended.

Hi, I tried your code and it gave me an error after that I tried to open the db in the window.plugins.sqlDB.copy error function. The error said “Error in Success callbackId: sqlDB1540206385 : Error: Database location or iosDatabaseLocation value is now mandatory in openDatabase call”. I don’t know how solves it and I don’t know how use a prefilled database. I tried all the guides which are in internet. Coincidentally, have you solved your problem?

Hi Grace,

Actually I figured out that using ionic persistence is better in my case. Anyway you can find here something more about this point:

If you search for my comments you find some answer that can help you.

Let me know if you solve and how. Good luck!

Hi Nadir. Finally I won and now I’m very happy. After a week where I tried everything, finally I succeed in it.
First of all I put the db in the folder www. Then I wrote all the code in my controller. The code is:

var dbName = “database_name.db”;
var db = null;
var query = “SELECT * FROM table_name”;
if (window.sqlitePlugin) { //device
//Copy and open the database
window.plugins.sqlDB.copy(dbName, 0, function () {
db = window.sqlitePlugin.openDatabase({name: dbName, location: 1}, function (db) {
db.executeSql(query,,function(result){
console.log(result.rows.length);
},function(err){
console.log(“Error:” + err.message);
});
}, function (err) {
console.log('Error opening the database: ’ + err.message);
});
}, function (error) {
console.error("Error copying the database: " + error.message);
db = window.sqlitePlugin.openDatabase({name: dbName, location: 1}, function (db) {
db.executeSql(query,,function(result){
console.log(result.rows.length);
},function(err){
console.log(“Error:” + err.message);
});
}, function (err) {
console.log('Error opening the database: ’ + err.message);
});
});
} else{ //browser
db = window.openDatabase(dbName, 1, dbName, -1);
}

I tried it with an android device and it works for me. I hope that it can help you and who needs! :smiley:

1 Like

Thankyou GracePii :smiley:

please paste the code in tag given by ionic in the top bar for more readability.:smiley: