javascript - ionic SQLite ;无法打开数据库

标签 javascript typescript sqlite ionic-framework

我想将我的 ionic 应用程序与数据库相关联,但我遇到了一些错误。 我正在使用 google chrome DevTools 在 android 设备上运行该应用程序以查看背后发生的情况。

查看 createDatabase() 函数和我遇到的错误。

createDatabase() {
    this.plt.ready().then(() => {
      if (this.plt.is('cordova')) {
        this.sqlite = new SQLite();
        this.sqlite.create({
          name: 'ocp_database_fuits.db',
          location: 'default'
        }).then((db: SQLiteObject) => {
          this.database = db;
          this.createTables();
        }).catch(e => {
          console.log(e);
        });
      }
    });
  }

OPEN database: ocp_database_fuits.db

SQLitePlugin.js:197 OPEN database: ocp_database_fuits.db

FAILED, aborting any pending transactions main.js:686 Error: Could not open database

  at newSQLError (SQLitePlugin.js:26)
  at SQLitePlugin.js:199
  at Object.callbackFromNative (cordova.js:290)
  at <anonymous>:1:9

最佳答案

好的。为了解决您的问题,请按照以下步骤操作

npm update
npm run build
ionic cordova plugin rm cordova-plugin-sqlite-2

之后,在您的数据库文件中更改它

createDatabase(): void {
this.plt.ready().then(() => {
  if (this.plt.is('cordova')) {
    this.sqlite = new SQLite();
    this.sqlite.create({
      name: 'ocp_database_fuits.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      this.database = db;
      this.createUserTable(this.database);
      this.createPathTable(this.database);
    }).catch(e => {
      console.log(e);
    });
  }
});
}

还有

createUserTable(db): void {
    // const d: SQLiteObject;
    db.executeSql('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, pass TEXT)', [])
    .then(() => {
      console.log('table users created');
    }).catch(e => {
      console.log('error in createUserTable function');
      console.log(e);
    });
  }

createPathTable(db): void {
    db.executeSql('CREATE TABLE IF NOT EXISTS paths (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT, points TEXT', [])
    .then(() => {
      console.log('table paths created');
    }).catch(e => {
      console.log('error in createPathTable function');
      console.log(e);
    });
  }

关于javascript - ionic SQLite ;无法打开数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193210/

相关文章:

javascript - Angular 6 - 日期管道

typescript - 如何使用 Typescript 将 TYPE 添加到 Object.assign

Python 表单/提示 SQLite 输入

javascript - jQuery 类不会添加更改选择框的选项

javascript - 为什么以及如何在 console.log 中引用变量来修复我遇到的加载错误?

javascript - Javascript html 中的 Windows 身份验证

angular - Angular 6中的FontAwesome图标?

SQL "IN subquery"当子查询可以为 NULL

android - Sqlite 中不存在列的返回类型?

javascript - 如何根据屏幕尺寸在 div 中显示不同的图像?