2
votes

I'm trying to implement this simple example given in ionic 2 doc: http://ionicframework.com/docs/v2/native/sqlite/

I tried that example (executing a query on a db placed under 'www\test.sqlite' folder of ionic project) on MAC and i get this error both on browser and on iOS simulator (doesn't work on device either):

ReferenceError: sqlitePlugin is not defined

I have added cordova-sqlite-storage plugin to the ionic project.

Code:

constructor(public navCtrl: NavController, public platform: Platform,
                public pps: ProdPerfService){
        platform.ready().then((readySource) => {
            pps.getSummary(); //pps is a provider named ProdPerfService
        });
    }

//ProdPerfService:
import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';

@Injectable()
export class ProdPerfService {
    constructor(){

    }

    getSummary(){
        let db = new SQLite();
        db.openDatabase({
            name: 'test.sqlite',
            location: 'default' // the location field is required
        }).then(() => {
            db.executeSql('select * from summary', {}).then(() => {
                alert('result');

        }, (err) => {
            console.error('Unable to execute sql: ', err);
            alert('err');
        })
        }, (err) => {
            console.error('Unable to open database: ', err);
            alert(err);
        });
    }

}

ionic details: Cordova CLI: 6.4.0 Ionic Framework Version: 2.0.0-rc.3 Ionic CLI Version: 2.1.17 Ionic App Lib Version: 2.1.7 Ionic App Scripts Version: 0.0.45 ios-deploy version: Not installed ios-sim version: Not installed OS: OS X El Capitan Node Version: v7.2.1 Xcode version: Xcode 8.1 Build version 8B62

2

2 Answers

0
votes

Try moving your logic out of constructor .

ionViewDidLoad(){
   this.platform.ready().then((readySource) => {
       this.pps.getSummary(); //pps is a provider named ProdPerfService
   });
}
0
votes

You need to add the next detail:

In your imports:

import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

And in getSummary() function:

getSummary(){
    let db = new SQLite();
    db.openDatabase({
        name: 'test.sqlite',
        location: 'default' // the location field is required
    }).then((sqlite: SQLiteObject) => {

I hope that this solves your issue.