sto*_*per 2 sqlite ionic-framework
我不明白为什么下面的代码会这样工作:所有结果都进入 catch 块。“select * from danceMoves”之后的最后一个 catch 块实际上会将“swag”写入控制台。
这是一个使用 ionic start 创建的简单项目,然后添加 cordova-sqlite-storage。遵循 Ionic 站点文档。
public testSQLite() {
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('create table if not exists danceMoves(name VARCHAR(32))')
.then(() => { debugger; console.error('Executed SQL') })
.catch(e => { debugger; console.error(e) })
.then(() => { debugger; return db.executeSql("insert into danceMoves(name) values('swag')") })
.then(() => { debugger; return db.executeSql("select * from danceMoves") })
.then(result => { debugger; console.log(result.rows.item(0).name) })
.catch(error => { debugger; console.error(error) })
.then(() => { debugger; return db.executeSql("select * from danceMoves") })
.then(result => { debugger; console.log(result.rows.item(0).name) })
.catch(result => { debugger; console.log(result.rows.item(0).name)});
})
.catch(e => { debugger; console.error(e) });
}
Run Code Online (Sandbox Code Playgroud)
好吧,我找到了一个可能的解决方案(至少它解决了我的问题)。
executeSql方法需要两个参数:一个语句和一个参数数组。
该语句可能包含参数(例如SELECT * FROM table WHERE id=?),这些参数使用 params 数组中给定的对象(例如[1])进行解析。
params 数组参数被标记为可选,因为可以在没有参数的情况下工作。但是,如果未给出数组,则查询的结果集将作为错误抛出,而不是返回它。为了解决这个问题,我总是指定一个空数组作为第二个参数,这为我解决了这个问题。
调用示例:
db.executeSql(SELECT * FROM danceMoves, []).then(result => console.log(result.rows.item(0).name)
| 归档时间: |
|
| 查看次数: |
1012 次 |
| 最近记录: |