无法打开数据库ReferenceError:未定义sqlitePlugin

时间:2016-08-29 05:51:27

标签: ios sqlite angular ionic2

我正在开发Angular2和Ionic2中的ios App。

我的系统信息是:

enter image description here

我的Product.ts文件代码是:

import { Component} from '@angular/core';
import {NavController, NavParams, Modal} from 'ionic-angular';
import {Platform, ionicBootstrap} from 'ionic-angular';
import {StatusBar, SQLite} from 'ionic-native';

@Component({
  templateUrl: 'build/pages/product/product.html',
})

export class ProductPage {
  public database: SQLite;
  public peoples: Array<Object>;

  constructor(public platform: Platform)
   {
    //this.createSqliteDB();
    //this.openDB();
    //this.addDB();
    //this.refreshDB();
    //this.showDBValue();
  }

  createSqliteDB()
  {
    console.log("createSqliteDB called.");

    let db = new SQLite();
    db.openDatabase({
        name: "data.db",
        location: "default"
        }).then(() => {
          db.executeSql("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)", {}).then((data) => {
          console.log("TABLE CREATED: ", data);
          }, (error) => {
            console.error("Unable to execute sql", error);
          })
          }, (error) => {
            console.error("Unable to open database", error);
      });



    }

  openDB()
  {
    console.log("openDB called.");

    this.database = new SQLite();
    this.database.openDatabase({name: "data.db", location: "default"}).then(() => {
      this.refreshDB();
    }, (error) => {
      console.log("openDB ERROR: ", error);
    });
  }

  public addDB() 
  {
    console.log("addDB called.");

    this.database.executeSql("INSERT INTO people (firstname, lastname) VALUES ('Nic', 'Raboy')", []).then((data) => {
            console.log("INSERTED: " + JSON.stringify(data));
        }, (error) => {
            console.log("addDB ERROR: " + JSON.stringify(error.err));
        });
    }

  public refreshDB() 
  {
    console.log("refreshDB called.");

    this.database.executeSql("SELECT * FROM people", []).then((data) => {
            this.people = [];
            if(data.rows.length > 0) {
                for(var i = 0; i < data.rows.length; i++) {
                    this.people.push({firstname: data.rows.item(i).firstname, lastname: data.rows.item(i).lastname});
                }
            }
        }, (error) => {
            console.log("refreshDB ERROR: " + JSON.stringify(error));
        });
  }

  showDBValue()
  {
    console.log("showDBValue called.");

    this.database = new SQLite();
        this.database.openDatabase({name: "data.db", location: "default"}).then(() => {
            this.refreshDB();
        }, (error) => {
            console.log("ERROR: ", error);
        });
  }

  getData()
  {
    console.log("getData called.");

    //this.database = new SQLite();
    //console.log("this.database obj: ",this.database);

    this.createSqliteDB();
    //this.openDB();
    //this.addDB();
    //this.refreshDB();
    //this.showDBValue();
  }
}

我已在应用程序中使用SQLite数据库为应用this code的本地存储并运行应用程序以显示此错误: enter image description here

0 个答案:

没有答案
相关问题