ionic cordovaSQLite plugin doesn't work on physical device

3k views Asked by At

I want to add SQLite data to my mobile application in ionic I had followed the tutorial step by step in the following site https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/ but still it dosen't work on physical device,any advice. in the index.html i put the following code `

<script src="lib/ionic/js/ionic.bundle.js"></script>

<script src="lib/ionic/js/angular-ui/angular-ui-router.js"></script>
<script src="lib/ionic/js/angular/angular-resource.js"></script>



<!-- Needed for Cordova/PhoneGap (will be a 404 during development) -->
<script src="js/app.js"></script>
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>`  

in my app.js I wrote the following code first code

     var db=null;
     var myApp=angular.module('myApp',['ionic','ngCordova'])
    .run(function($ionicPlatform, $cordovaSQLite) {
     $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
        StatusBar.styleDefault();
    }

    db = $cordovaSQLite.openDB("test.db");
    $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");
    console.log("hello");
})

})

so I have created a table named people and in my login controller I'll insert one row to people table

controller('loginController',function($scope,$cordovaSQLite){

    var query = "INSERT INTO people (id,firstname, lastname) VALUES (?,?,?)";
    $cordovaSQLite.execute(db, query, [1,"khaled", "omar"]).then(function(res) {
        $scope.name=res.insertId;
    }, function (err) {
        $scope.name="error";
    });

and in the login.html i wrote {{name}} but when i run it on browser i get the followin error Cannot read property 'openDatabase' of undefined and I run it on the physical device but still doesn't work

5

There are 5 answers

0
sami On

The code below works for me.

I don't know why, but you have to put your database creation code inside the onReady method before the if statement like this:

.run(function($ionicPlatform,$cordovaSQLite) {
  $ionicPlatform.ready(function() {  

    db = $cordovaSQLite.openDB({name:"my.db", location:1});              
          $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS testTable(id integer primary key, col1 text,col2 tex)");

    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }                        
  });
0
Dan Bucholtz On

$ionicPlatform.ready() must fire before you can use cordova.

0
khaled el omar On

the code is working well, it seems the error comes from a problem in the controller.

0
Samuel Veizaga On

This actually worked, thanks musakkhir and anuj

db = $cordovaSQLite.openDB({name: "dbname.db", location: 1});

location:1 is important.

0
Anuj On

You should use following code.

db = $cordovaSQLite.openDB({name: "dbname.db", location: 1});

It will work.