首页 文章

sqlitePlugin在离子2中使用SQLite时出错

提问于
浏览
2

我正在尝试实现离子2 doc中给出的这个简单示例:http://ionicframework.com/docs/v2/native/sqlite/

我在 MAC 上尝试了这个例子(在离子项目的 'www\test.sqlite' 文件夹下的数据库上执行查询),我在浏览器和iOS模拟器上都得到了这个错误(在设备上也不起作用):

ReferenceError: sqlitePlugin is not defined

我已经将cordova-sqlite-storage插件添加到离子项目中 .

码:

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);
        });
    }

}

离子细节:Cordova CLI:6.4.0离子框架版本:2.0.0-rc.3离子CLI版本:2.1.17离子应用程序库版本:2.1.7离子应用程序脚本版本:0.0.45 ios-deploy版本:不安装ios-sim版本:未安装OS:OS X El Capitan Node版本:v7.2.1 Xcode版本:Xcode 8.1 Build版本8B62

2 回答

  • 0

    您需要添加下一个细节:

    在你的进口:

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

    并在getSummary()函数中:

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

    我希望这能解决你的问题 .

  • 0

    尝试将逻辑移出构造函数 .

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

相关问题