Sqlite is not working with ionic2, again


#1

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.


#2

and you installed the sqlite plugin?

ionic plugin add cordova-sqlite-storage

#3

Yes. both with --save and without. I also checked that the library is actually installed in the node_modules. And the storage module works.

$ cordova plugin add cordova-sqlite-storage --save
$npm install --save @ionic/storage
import { Storage } from '@ionic/storage';

@NgModule({
  declarations: [
    // ...
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    // ...
  ],
  providers: [
    Storage
  ]
})
export class AppModule {}
import { Storage } from '@ionic/storage';

export class MyApp {
  constructor(storage: Storage) {
     storage.set('name', 'Max');
     storage.get('name').then((val) => {
       console.log('Your name is', val);
     })
  }
}

This module is using the exact same plugin. And it works. But when I tried to follow the doc on SQlite( http://ionicframework.com/docs/v2/native/sqlite/ ). It keeps saying cannot find plugin.

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.


#4

Are you opening database after deviceready are fired?


#5

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.


#6

I think it works in IOS emulator after all. Finally find a way to test it.