Reputation: 4859
I want to work with sqlite with cordova framework for the first time. As I've read on a tutorial I should use ngcordova like this:
var db = null;
app.controller('mainCtrl', function ($scope, $ionicSideMenuDelegate) {
$scope.toggleLeft = function () {
$ionicSideMenuDelegate.toggleLeft();
}
$scope.toggleRight = function () {
$ionicSideMenuDelegate.toggleRight();
}
})
.controller('home_ctrl', function ($scope, $cordovaSQLite) {
db = $cordovaSQLite.openDB({name: "my.db"});
//db = $window.opendb({name: "my.db"});
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people(id integer primary key, firstname text, lastname text)")
})
;
When I run this code an error says:
TypeError: Cannot read property 'openDatabase' of undefined
In some articles like this: How do I use the ngCordova sqlite service and the Cordova-SQLitePlugin with Ionic Framework?
recommends to use commands like this: ionic start myApp sidemenu
I don't know what is it, I just use cordova command like: cordova run android
or cordova create my_project
What should I do to run my create a table from my cordova project?
Upvotes: 12
Views: 29269
Reputation: 719
Actually, I opened an old project and I get this error only when I run the project with -cls (because of liverelaod). But I have a small bug to fix there so I didn't investigate it too much(just fix it without livereload). But maybe this little hint helps somebody.
Upvotes: 0
Reputation: 3153
You might take a look at this tutorial:
https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/
When you see this error:
TypeError: Cannot read property 'openDatabase' of undefined
It is happening for one of a few reasons:
$cordovaSQLite
methods in the $ionicPlatform.ready()
function.The most common reasons for this error are #1 and #2. Native plugins, must be used only after the application is confirmed ready, thus the $ionicPlatform.ready()
method. Since native plugins use native code, you cannot test them from your web browser.
Read through the tutorial I linked because it should help you.
Regards,
Upvotes: 51