【问题标题】:Ionic 3 SQLite: SQLiteObject's method executeSql() not workingIonic 3 SQLite:SQLiteObject 的方法 executeSql() 不起作用
【发布时间】:2019-02-07 19:02:22
【问题描述】:

我做了什么:

  1. 今天安装了 ionic,通过命令 npm install -g ionic 并对 cordova 集成说“y”。
  2. 安装的科尔多瓦:npm install -g cordova
  3. 创建了一个新项目:ionic start sqlite3demo blank
  4. 已安装 Ionic 原生存储:

    ionic cordova plugin add cordova-sqlite-storage

    npm install --save @ionic-native/sqlite

现在是代码。我像这样将SQLite 导入app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { SQLite } from '@ionic-native/sqlite';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    SQLite
  ]
})
export class AppModule {}

然后,我像这样修改了默认主页:

<ion-header>
  <ion-navbar>
    <ion-title>
      SQLite demo
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <ion-content>

    <button ion-button (click)="createTables()">Create db</button>
    <p>Result: {{ message }}</p>

  </ion-content>
</ion-content>

最后,我像这样修改了home.ts

import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  message = '';
  db: SQLiteObject;

  constructor(private platform: Platform, public navCtrl: NavController, private sqlite: SQLite) {
    this.platform.ready().then(() => {
      this.sqlite.create({
        name: 'todos.db',
        location: 'default'
      })
        .then((db: SQLiteObject) => {
          this.message = JSON.stringify(db);
          this.db = db;
        });
    });
  }

  public createTables() {
    return this.db.executeSql(
      `CREATE TABLE IF NOT EXISTS list (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT
      );`)
      .then(() => this.message = 'OK')
      .catch((err) => this.message = "error detected creating tables -> " + JSON.stringify(err));
  }

}

在这之后,我执行ionic cordova run android,我可以看到来自db 的 JSON,但在那之后,我单击按钮创建一个表,而不是看到“确定”,我看到来自错误。

我做错了什么?为什么它不起作用?

编辑:我使用的版本是:

"@ionic-native/sqlite": "^4.12.2",
"cordova-sqlite-storage": "^2.4.0"

【问题讨论】:

    标签: sqlite cordova ionic-framework ionic-storage


    【解决方案1】:

    请注意CREATE TABLE如果表已经存在则抛出异常。
    CREATE TABLE IF NOT EXISTS查询将在表不存在时创建表,否则通过抛出异常简单地忽略命令。
    如果您想创建一个表,那么您需要在CREATE TABLE IF NOT EXISTS 之前使用DROP TABLE IF EXISTS 删除现有表。

    【讨论】:

    • 这毫无意义。在我到目前为止阅读的所有示例中,人们使用CREATE TABLE IF NOT EXISTS 而不使用指令DELETE TABLE IF EXISTS(顺便说一句,它是DROP TABLE 而不是DELETE TABLE)。我已经在这方面投入了几个小时,我开始认为 SQLite 插件只是简单的错误,至少是它的最后一个版本。我现在使用的版本在方法调用结束时不需要空对象{},不像我目前阅读的所有示例。
    【解决方案2】:

    最后,正如here 所描述的那样,文档已过时。 executeSql() 的工作方式是将 [] 作为第二个参数传递。似乎 Ionic Native 团队并没有考虑过多地更新他们的文档。还有其他插件也有过时的文档,比如email composer,这使得开发人员在已经有解决方案的事情上浪费了很多时间。

    【讨论】:

      猜你喜欢
      • 2018-10-07
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多