ngCordova + SQLite plugin issue in IOS

I got an error with the $cordovaSQLite:
Error: ‘undefined’ is not a function (evaluating ‘e.transaction(function(n){n.executeSql(o,r,function(n,e){q.resolve(e)},function(n,e){q.reject(e)})})’) at ionic.bundle.js:18897

and in sequence the console gives:
execute@file:///myPath/myApp.app/www/js/ng-cordova.min.js:1:14061

here the code in my controller:

$scope.dbTest = function(){
    var db = 'testing';
    var query = '';
    $cordovaSQLite.openDB(db);
    query = 'CREATE TABLE IF NOT EXISTS MyTable (id INTEGER PRIMARY KEY ASC, text_sample TEXT, date_sample DATE)';
    $cordovaSQLite.execute(db, query, [])
    .then(function(data){
                  console.log(data);
                  });

    query = 'INSERT INTO MyTable(text_sample, date_sample) VALUES ("Test insertion","2014-05-28")';
    $cordovaSQLite.execute(db, query, [])
    .then(function(data){
                  console.log(data);
                  });

    query = 'SELECT * FROM MyTable ORDER BY id';
    $cordovaSQLite.execute(db, query, [])
    .then(function(data){
                  console.log(data);
                  });
  }

anyone have success with the SQLite plugin + ngCordova? if yes, could help me? or fix some mistake in my code…

thank you!..

I’m using the last launch: 1.0.0-beta.9 Gadolinium Gator (2014-07-02)
testing in iOS simulator and device… the DB is opened, but I can’t run the
$cordovaSQLite.execute(db, query, binding) function…

What order do you have your scripts loading?

first ionic.bundle.js
and ng-cordova.min.js after

Where’s cordova.js in this?

is the last file loaded…

var dbName = ‘testing’;
var query = ‘’;
var db = $cordovaSQLite.openDB(dbName);

hey @fleajump thank you very much! :smiley:

now is working! :smile:

I just can’t get the data from the SELECT query… how can I do that? I would like to put the query result in a $scope.result variable…

thank you again for your time!.. :blush:

var dbName = ‘testing.db’;

query = 'SELECT * FROM MyTable ORDER BY id';
$cordovaSQLite.execute(db, query, [])
    .then(function(data){
    	for (var i = 0; i < data.rows.length; i++) {
    	    $scope.result = data.rows.item(i);
    	    console.log($scope.result);
    	}
    });
1 Like

Hi @fleajump and @fdedeh, thanks for all your help! Would you know if there is any way to open a prepopulated SQLite DB? One which is already present as a file, suppose test.db in the /www/ folder?
Thanks so much!

hey @fleajump, thx again for your time! :smile:

@shubs, I read a tutorial some time ago where the author explains that to open a prepopulated DB in SQLite you need just insert the .db file in your project and specify the full path to it when you open the DB… something like

var dbName = 'your/path/testing.db';

I searched for this tutorial again but unfortunately I can’t find it… :confused:

Please all, if I’m wrong, correct me…

1 Like

Download the prepopulated SQLite DB from the www folder to the Documents folder.

var dbName = 'test.db';
var source = cordova.file.applicationDirectory + 'www/db/' + dbName;
var filePath = cordova.file.documentsDirectory + dbName;

$cordovaFile.downloadFile(source, filePath, true).then(function(result) {
    var db = $cordovaSQLite.openDB(dbName);
}, function(err) {
    console.log(err);
});
2 Likes

Please make sure you are not using the old version. Take a look at the ngCordova.js and make sure the ** line (line 1211) is modified.

execute: function (db, query, binding) {
        **q = $q.defer();**//should be var q = $q.defer();
        db.transaction(function (tx) {
          tx.executeSql(query, binding, function (tx, result) {
              q.resolve(result);
            },
            function (transaction, error) {
              q.reject(error);
            });
        });
        return q.promise;
      }

Try this => https://github.com/jdnichollsc/Ionic-Starter-Template

Regards, Nicholls