gpt4 book ai didi

javascript - 如何使用通用函数将数据插入到 sqlite 数据库中

转载 作者:行者123 更新时间:2023-12-02 23:52:16 24 4
gpt4 key购买 nike

我正在编码的函数将从其他 javascript 文件调用,并且字段和值将传递给函数,包括表名称。但是当我运行该函数时,它只是给出错误,没有此错误消息

    We have encounter an Error WebSQLTransaction { "_complete": false, "_error": null, "_running": true, "_runningTimeout": false, "_sqlQueue": Queue { "first": undefined, "last": undefined, "length": 0, }, "_websqlDatabase": WebSQLDatabase { "_currentTask": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, "_db": SQLiteDatabase { "_closed": false, "_name": "mydb.db", }, "_running": true, "_txnQueue": Queue { "first": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, "next": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, "next": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, }, }, }, "last": Object { "item": TransactionTask { "errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous], }, }, "length": 3, }, "version": "1.0", }, }

所以我使用 javascript React Native 构建这个并使用 expo SDK。 Expo 附带在 SQLite 模块中,我所做的又是构建一个可以从代码中的任何位置调用的函数,并将一些数据存入数据库中。

   export const insert = (tbl, fields, values) =>{
const query = "insert into ${tbl} (${fields}) values (${values});";
console.log(query);
//it looks fine to me
db.transaction(trx => {
let trxQuery = trx.executeSql(
query
,[values],(data)=> console.log('we made it',data),(err)=>console.log('We have encounter an Error', err))
console.log(trxQuery); // retruns undefined
})
}

这就是我调用该函数的方式

    const personObj = JSON.parse(personDetails);
Object.keys(personObj).map(i =>
insert('users','name, address, hash', [personObj[i].name, personObj[i].address, personObj[i].hash])
)

我希望获得我们做到了的控制台日志以及有关我们插入的数据的信息。感谢您的帮助

最佳答案

成功和错误回调有两个参数,第一个是事务本身,第二个是错误或结果集。您应该像下面一样编辑代码并检查结果集对象,

`export const insert = (tbl, fields, values) =>{
const query = "insert into ${tbl} (${fields}) values (${values});";
console.log(query);
//it looks fine to me
db.transaction(trx => {
let trxQuery = trx.executeSql(
query
,[values]
,(transact,resultset) => console.log('we made it',resultset)
,(transact,err) => console.log('We have encounter an Error', err)
);
})
}`

关于javascript - 如何使用通用函数将数据插入到 sqlite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com