我正在使用离子Sqlite本机插件。尝试创建表时,我在控制台中收到此错误:
“控制台消息错误:{“行”:{“长度”:0},“行受影响”:0}”
我不理解,因为我认为我的代码还可以。
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from "@ionic-native/sqlite";
@Injectable()
export class DatabaseProvider {
theConsole: string = "Console Messages";
options: any = {
name: 'db_name.db',
location: 'default'
}
private db: SQLiteObject;
private isOpen: boolean;
constructor(public http: HttpClient,private sqlite: SQLite) {
console.log('Hello DatabaseProvider Provider');
this.connectToDb();
}
private connectToDb():void {
this.sqlite = new SQLite();
this.sqlite.create(this.options)
.then((db: SQLiteObject) => {
this.db = db;
this.isOpen = true;
console.log('Hello DatabaseProvider connected to db');
this.createTables();
})
.catch(e => {
this.theConsole += JSON.stringify(e);
console.log(this.getConsoleMessages());
});
}
private createTables():void{
this.createTableContatti();
}
private createTableContatti(): void{
var sql = 'create table IF NOT EXISTS contatti(id_contatto INTEGER PRIMARY
KEY AUTOINCREMENT,nome TEXT, data_nascita TEXT)';
this.db.executeSql(sql)
.then(() => {
this.theConsole += 'Executed SQL' + sql
console.log(this.getConsoleMessages());
})
.catch(e => {
this.theConsole += "Error: " + JSON.stringify(e)
console.log(this.getConsoleMessages());
});
}
有人可以帮忙吗? 预先谢谢你
答案 0 :(得分:1)
离子型SQLite的executeSql需要2个参数查询和数据。如果没有数据,则传递一个空数组对象。
var sql = 'create table IF NOT EXISTS contatti(id_contatto INTEGER PRIMARY
KEY AUTOINCREMENT,nome TEXT, data_nascita TEXT)';
this.db.executeSql(sql,{})
.then(() => {
this.theConsole += 'Executed SQL' + sql
console.log(this.getConsoleMessages());
})
.catch(e => {
this.theConsole += "Error: " + JSON.stringify(e)
console.log(this.getConsoleMessages());
});
如果您传递第二个参数,您的查询将正常工作。