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
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 theif
statement like this: