2

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

khaled el omar
  • 149
  • 1
  • 4
  • 13
  • I don't think you'll get any answer with this much information. You should probably show your code, what is expected, what are the errors etc. – AtanuCSE Dec 23 '14 at 08:37
  • ok i wrote the details, thank you for your advice – khaled el omar Dec 23 '14 at 09:01
  • Glad you used my tutorial! If you look carefully at `db = $cordovaSQLite.openDB("test.db");` you'll notice that is not correct. The parameter is not a string. It needs to be an object. Try putting `{ name: "test.db" }` in there instead and let us know how it goes. – Nic Raboy Dec 24 '14 at 16:24
  • @Nic: on your tutorial you write "test.db" and not the object. What is the correct one? – WJA Apr 01 '15 at 16:08
  • I believe the API changed and you want to use a string, not an object. – Nic Raboy Apr 02 '15 at 02:35

5 Answers5

1

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

khaled el omar
  • 149
  • 1
  • 4
  • 13
1

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();
    }                        
  });
Pang
  • 9,564
  • 146
  • 81
  • 122
sami
  • 231
  • 2
  • 8
0

You should use following code.

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

It will work.

Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
Anuj
  • 1
0

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

Dan Bucholtz
  • 713
  • 1
  • 5
  • 11
0

This actually worked, thanks musakkhir and anuj

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

location:1 is important.

Community
  • 1
  • 1