在 ionic 4 中创建 sqlite 语句时出错
Error when creating an sqlite statement in ionic 4
我正在学习有关 IONIC 4 CRUD 的教程。当我到达 sql 语句时,出现如下错误:
> Argument of type '{}' is not assignable to parameter of type 'any[]'.
> Type '{}' is missing the following properties from type 'any[]':
> length, pop, push, concat, and 26 more.ts
这是我收到错误的语句:
db.executeSql('CREATE TABLE IF NOT EXISTS expense(rowid INTEGER PRIMARY KEY, date TEXT, type TEXT, description TEXT, amount INT)', {})
db.executeSql('SELECT SUM(amount) AS totalIncome FROM expense WHERE type="Income"', {})
db.executeSql('SELECT SUM(amount) AS totalExpense FROM expense WHERE type="Expense"', {})
我得到“{}”上的红线
这是完整的代码:
getData() {
this.sqlite.create({
name: 'ionicdb.db',
location: 'default'
}).then((db: SQLiteObject) => {
db.executeSql('CREATE TABLE IF NOT EXISTS expense(rowid INTEGER PRIMARY KEY, date TEXT, type TEXT, description TEXT, amount INT)', {})
.then(res => console.log('Executed SQL'))
.catch(e => console.log(e));
db.executeSql('SELECT * FROM expense ORDER BY rowid DESC', {})
.then(res => {
this.expenses = [];
for(var i=0; i<res.rows.length; i++) {
this.expenses.push({rowid:res.rows.item(i)
.rowid,date:res.rows.item(i).date,type:res.rows.item(i)
.type,description:res.rows.item(i).description,amount:res.rows.item(i).amount});
}
})
.catch(e => console.log(e));
db.executeSql('SELECT SUM(amount) AS totalIncome FROM expense WHERE type="Income"', {})
.then(res => {
if(res.rows.length>0) {
// tslint:disable-next-line: radix
this.totalIncome = parseInt(res.rows.item(0).totalIncome);
this.balance = this.totalIncome - this.totalExpense;
}
})
.catch(e => console.log(e));
db.executeSql('SELECT SUM(amount) AS totalExpense FROM expense WHERE type="Expense"', {})
.then(res => {
if(res.rows.length>0) {
// tslint:disable-next-line: radix
this.totalExpense = parseInt(res.rows.item(0).totalExpense);
this.balance = this.totalIncome - this.totalExpense;
}
});
}).catch(e => console.log(e));
}
我需要你的帮助。谢谢。
你应该通过 []
而不是 {}
。
db.executeSql('create table danceMoves(name VARCHAR(32))', [])
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
我正在学习有关 IONIC 4 CRUD 的教程。当我到达 sql 语句时,出现如下错误:
> Argument of type '{}' is not assignable to parameter of type 'any[]'.
> Type '{}' is missing the following properties from type 'any[]':
> length, pop, push, concat, and 26 more.ts
这是我收到错误的语句:
db.executeSql('CREATE TABLE IF NOT EXISTS expense(rowid INTEGER PRIMARY KEY, date TEXT, type TEXT, description TEXT, amount INT)', {})
db.executeSql('SELECT SUM(amount) AS totalIncome FROM expense WHERE type="Income"', {})
db.executeSql('SELECT SUM(amount) AS totalExpense FROM expense WHERE type="Expense"', {})
我得到“{}”上的红线
这是完整的代码:
getData() {
this.sqlite.create({
name: 'ionicdb.db',
location: 'default'
}).then((db: SQLiteObject) => {
db.executeSql('CREATE TABLE IF NOT EXISTS expense(rowid INTEGER PRIMARY KEY, date TEXT, type TEXT, description TEXT, amount INT)', {})
.then(res => console.log('Executed SQL'))
.catch(e => console.log(e));
db.executeSql('SELECT * FROM expense ORDER BY rowid DESC', {})
.then(res => {
this.expenses = [];
for(var i=0; i<res.rows.length; i++) {
this.expenses.push({rowid:res.rows.item(i)
.rowid,date:res.rows.item(i).date,type:res.rows.item(i)
.type,description:res.rows.item(i).description,amount:res.rows.item(i).amount});
}
})
.catch(e => console.log(e));
db.executeSql('SELECT SUM(amount) AS totalIncome FROM expense WHERE type="Income"', {})
.then(res => {
if(res.rows.length>0) {
// tslint:disable-next-line: radix
this.totalIncome = parseInt(res.rows.item(0).totalIncome);
this.balance = this.totalIncome - this.totalExpense;
}
})
.catch(e => console.log(e));
db.executeSql('SELECT SUM(amount) AS totalExpense FROM expense WHERE type="Expense"', {})
.then(res => {
if(res.rows.length>0) {
// tslint:disable-next-line: radix
this.totalExpense = parseInt(res.rows.item(0).totalExpense);
this.balance = this.totalIncome - this.totalExpense;
}
});
}).catch(e => console.log(e));
}
我需要你的帮助。谢谢。
你应该通过 []
而不是 {}
。
db.executeSql('create table danceMoves(name VARCHAR(32))', [])
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));