【问题标题】:Ionic 2 SQLite not opening databaseIonic 2 SQLite 未打开数据库
【发布时间】:2017-02-09 05:12:54
【问题描述】:

我正在使用 Ionic 2,并关注 this tutorial

我的问题是我似乎无法打开数据库。我已经将它部署到一个名为 KOPLAYER 的 Android 模拟器上。

app.ts

  initializeApp() {
    this.platform.ready().then(() => {
      StatusBar.styleDefault();
      if (window.cordova) {
        this.createDatabase();
      }
    });
  }

  private createDatabase(): void {
    let db: SQLite = new SQLite();
    db.openDatabase({
      name: "data.db",
      location: "default"
    }).then(() => {
      db.executeSql("CREATE TABLE IF NOT EXISTS chats (_id TEXT PRIMARY KEY, memberIds TEXT, title TEXT, subTitle TEXT, picture TEXT, lastMessageId TEXT, lastMessageCreatedAt DATE)", {}).then((chatData) => {
        console.log("chats TABLE CREATED: ", chatData);
        db.executeSql("CREATE TABLE IF NOT EXISTS messages (_id TEXT PRIMARY KEY, chatId TEXT, senderId TEXT, ownership TEXT, content TEXT, createdAt DATE, changeDate BOOLEAN, readByReceiver BOOLEAN)", {}).then((messageData) => {
          console.log("messages TABLE CREATED: ", messageData);
        }, (error) => {
          console.error("Unable to execute messages sql", error);
        });

      }, (error) => {
        console.error("Unable to execute chats sql", error);
      });
    }, (error) => {
      console.error("Unable to open database", error);
    });
  }

storageService.ts

public database: SQLite;

constructor() {
    if (window.cordova) {
        this.openDatabase();
    }
}

private openDatabase(): void {
    console.log('openDatabase');
    this.database.openDatabase({ name: "data.db", location: "default" }).then(() => {
        this.refreshChats();
        this.refreshMessages();
    }, (error) => {
        console.log("ERROR: ", error);
    });
}

openDatabase 被输出到控制台,但this.refreshChats(); 没有被调用。根据控制台日志,app.ts 中的数据库创建看起来是正确的。

OPEN database: data.db SQLitePlugin.js:175
OPEN database: data.db - OK SQLitePlugin.js:179
chats TABLE CREATED:  Object app.bundle.js:215
messages TABLE CREATED:  Object app.bundle.js:217

然后

openDatabase

我使用chrome://inspect/#devices 来查看控制台输出。还有其他工具可以用来查看模拟器上运行的数据库吗?

任何帮助表示赞赏。

【问题讨论】:

    标签: sqlite ionic2


    【解决方案1】:

    我的错,我忘了this.database = new SQLite();

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

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      相关资源
      最近更新 更多