I’m trying to use sqlite in ionic2 app. I tried all the suggestions I can find with google and stackoverflow. Nothing works. It keeps throwing the error from this code, which is just copy-paste from http://ionicframework.com/docs/v2/native/sqlite/ page.
openDB() {
this.db.openDatabase({
name: 'data.db',
location: "default" // the location field is required
}).then(() => {
this.db.executeSql('create table danceMoves(name VARCHAR(32))', {}).then(() => {
}, (err) => {
console.error('Unable to execute sql: ', err);
});
}, (err) => {
console.error('Unable to open database: ', err);
});
}
the error is
"Unable to open database: ReferenceError: sqlitePlugin is not defined(…)"
coming from the last console.error.
if anyone has any success trying to use this plugin, please share. Thanks in advance.
And btw I tried both with IOS emulator and browser. Both platforms fail to open database. It’s a raw app. I just generated it to test sqlite only. There isn’t anything else in the app to conflict with that.
I’m using angular2. I don’t think “deviceready” is the way the framework works in angular 2. However, I do have
export class MyApp {
@ViewChild(Nav) nav: Nav;
// make HelloIonicPage the root (or first) page
rootPage: any = HelloIonicPage;
pages: Array<{ title: string, component: any }>;
sqlInitialized:Boolean = false;
constructor(
public platform: Platform,
public menu: MenuController,
public db: SqLiteService,
) {
this.initializeApp();
// set our app's pages
this.pages = [
{ title: 'Hello Ionic', component: HelloIonicPage },
{ title: 'My First List', component: ListPage }
];
}
initializeApp() {
this.platform.ready().then(() => {
this.db.openDB();
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
});
}
I called the method after “platform.ready()”. You can see the comments by Ionic core team saying that all plugins will be available there. And to be 100% sure. I also tried setTimout up to 10 second before firing the method call.