Ionic 3 SQLite:SQLiteObject 的方法 executeSql() 不工作
Ionic 3 SQLite: SQLiteObject's method executeSql() not working
我做了什么:
- 今天安装了 ionic,通过命令
npm install -g ionic
并向 cordova 集成说 'y'。
- 已安装的科尔多瓦:
npm install -g cordova
- 创建了一个新项目:
ionic start sqlite3demo blank
已安装的 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,但在那之后,我点击按钮创建一个table我没有看到 'OK',而是从错误中看到 JSON。
我做错了什么?为什么它不起作用?
编辑: 我使用的版本是:
"@ionic-native/sqlite": "^4.12.2",
"cordova-sqlite-storage": "^2.4.0"
请注意,如果 table 已经存在,CREATE TABLE
将抛出异常。
CREATE TABLE IF NOT EXISTS
查询将创建 table 如果它不存在,否则简单地通过抛出异常来忽略该命令。
如果您想创建一个 table,那么您需要在 CREATE TABLE IF NOT EXISTS
.
之前使用 DROP TABLE IF EXISTS
删除现有的 table
最后,正如 here 所述,文档已过时。 executeSql()
的工作方式是将 []
作为第二个参数传递。看来 Ionic Native 团队并没有考虑过多地更新他们的文档。还有其他带有过时文档的插件,例如 email composer
,让开发人员在已经有解决方案的事情上浪费了很多时间。
我做了什么:
- 今天安装了 ionic,通过命令
npm install -g ionic
并向 cordova 集成说 'y'。 - 已安装的科尔多瓦:
npm install -g cordova
- 创建了一个新项目:
ionic start sqlite3demo blank
已安装的 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,但在那之后,我点击按钮创建一个table我没有看到 'OK',而是从错误中看到 JSON。
我做错了什么?为什么它不起作用?
编辑: 我使用的版本是:
"@ionic-native/sqlite": "^4.12.2",
"cordova-sqlite-storage": "^2.4.0"
请注意,如果 table 已经存在,CREATE TABLE
将抛出异常。
CREATE TABLE IF NOT EXISTS
查询将创建 table 如果它不存在,否则简单地通过抛出异常来忽略该命令。
如果您想创建一个 table,那么您需要在 CREATE TABLE IF NOT EXISTS
.
DROP TABLE IF EXISTS
删除现有的 table
最后,正如 here 所述,文档已过时。 executeSql()
的工作方式是将 []
作为第二个参数传递。看来 Ionic Native 团队并没有考虑过多地更新他们的文档。还有其他带有过时文档的插件,例如 email composer
,让开发人员在已经有解决方案的事情上浪费了很多时间。