无法读取未定义的 sqlite ionic-v3 和 SQLite 的 属性 'split'
cannot read property 'split' of undefined sqlite ionic-v3 & SQLite
我正在使用 ionic-v3 和 sqlite 插件。我正在尝试创建简单的数据库并在其中插入一条记录,然后通过 SELECT 查询检索该记录。
我遇到的问题是数据库不会创建,并且会显示错误“无法读取未定义的 sqlite 的 属性 'split'”
这将在我第一次编写 try & catch 时出现在代码中。
$ ionic cordova plugin add cordova-sqlite-storage
$ npm install @ionic-native/sqlite
import { Component } from '@angular/core';
import { NavController, ToastController, Platform } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private db: SQLiteObject;
public movies: string[] = [];
public message:any;
constructor(public navCtrl: NavController, private toastCtrl: ToastController, private sqlite: SQLite, platform: Platform) {
platform.ready().then(() => {
this.CreateDatabase();
});
}
CreateDatabase()
{
try
{
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('create table IF NOT EXISTS Movies(id INTEGER PRIMARY KEY, name VARCHAR(32))', [])
.then(() => {
console.log('Table Movies created !');
this.db.executeSql('INSERT INTO `Movies` VALUES (`1`, `James Bond 007`)', [])
.then(() => console.log('Executed SQL'))
.catch(e => this.message = e);
})
.catch(e => this.message = e);
})
.catch(e => this.message = e);
}
catch(err)
{
this.presentToast(err);
}
}
public retrieveFilms() {
try
{
this.db.executeSql('SELECT name FROM `Movies`', [])
.then((data) => {
if(data == null) {
return;
}
if(data.rows) {
if(data.rows.length > 0) {
for(var i = 0; i < data.rows.length; i++) {
this.movies.push(data.rows.item(i).name);
}
}
}
}).catch(e => this.presentToast(JSON.stringify(e)));
}
catch(err)
{
this.presentToast(err);
}
}
presentToast(messageText) {
let toast = this.toastCtrl.create({
message: messageText,
duration: 3000,
position: 'bottom'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
}
我做错了什么?我已经在 Whosebug 中尝试了所有内容,甚至在 youtube 中的教程或 ionic 文档中都进行了尝试。但对我没有任何用处!
使用旧的 ionic-native/sqlite 版本 4.x.x 而不是 5.x.x。
我在使用最新的 ionic-native/sqlite 和 Ionic 3 时遇到了同样的问题。
使用 npm 卸载旧插件或在 package.json 文件中更改它。
并且您需要更改行:
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
到
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
我正在使用 ionic-v3 和 sqlite 插件。我正在尝试创建简单的数据库并在其中插入一条记录,然后通过 SELECT 查询检索该记录。
我遇到的问题是数据库不会创建,并且会显示错误“无法读取未定义的 sqlite 的 属性 'split'” 这将在我第一次编写 try & catch 时出现在代码中。
$ ionic cordova plugin add cordova-sqlite-storage
$ npm install @ionic-native/sqlite
import { Component } from '@angular/core';
import { NavController, ToastController, Platform } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private db: SQLiteObject;
public movies: string[] = [];
public message:any;
constructor(public navCtrl: NavController, private toastCtrl: ToastController, private sqlite: SQLite, platform: Platform) {
platform.ready().then(() => {
this.CreateDatabase();
});
}
CreateDatabase()
{
try
{
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('create table IF NOT EXISTS Movies(id INTEGER PRIMARY KEY, name VARCHAR(32))', [])
.then(() => {
console.log('Table Movies created !');
this.db.executeSql('INSERT INTO `Movies` VALUES (`1`, `James Bond 007`)', [])
.then(() => console.log('Executed SQL'))
.catch(e => this.message = e);
})
.catch(e => this.message = e);
})
.catch(e => this.message = e);
}
catch(err)
{
this.presentToast(err);
}
}
public retrieveFilms() {
try
{
this.db.executeSql('SELECT name FROM `Movies`', [])
.then((data) => {
if(data == null) {
return;
}
if(data.rows) {
if(data.rows.length > 0) {
for(var i = 0; i < data.rows.length; i++) {
this.movies.push(data.rows.item(i).name);
}
}
}
}).catch(e => this.presentToast(JSON.stringify(e)));
}
catch(err)
{
this.presentToast(err);
}
}
presentToast(messageText) {
let toast = this.toastCtrl.create({
message: messageText,
duration: 3000,
position: 'bottom'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
}
我做错了什么?我已经在 Whosebug 中尝试了所有内容,甚至在 youtube 中的教程或 ionic 文档中都进行了尝试。但对我没有任何用处!
使用旧的 ionic-native/sqlite 版本 4.x.x 而不是 5.x.x。 我在使用最新的 ionic-native/sqlite 和 Ionic 3 时遇到了同样的问题。
使用 npm 卸载旧插件或在 package.json 文件中更改它。
并且您需要更改行:
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
到
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';