gpt4 book ai didi

javascript - react native SQLite 插入语句不起作用

转载 作者:行者123 更新时间:2023-11-30 20:00:08 39 4
gpt4 key购买 nike

我正在尝试将 SQLite 与 React Native 结合使用以将一些值插入表中

在我的主页中,我正在创建这样的表...

db.transaction(tx => {
tx.executeSql(
`create table if not exists puzzles (
id primary key not null,
level int not null,
image varchar(512) NOT NULL,
imageSolution varchar(512) NULL,
puzzleAnswer varchar(16) NULL,
type varchar(16) NULL,
availableLetters varchar(16) NULL,
charactersGiven varchar(4) NULL);`
);
});

在我的页面组件中,我尝试使用以下内容插入:

    db.transaction(tx => {
for (var puz of puzzlesFiltered) {
tx.executeSql(
`insert into
puzzles (id, image, imageSolution, type, puzzleAnswer, level, charactersGiven, availableLetters)
VALUES (?,?,?,?,?,?,?,?) WHERE NOT EXISTS(SELECT 1 FROM puzzles WHERE id = ${
puz.id
})`,
[
puz.id,
puz.image,
puz.imageSolution,
puz.type,
puz.puzzleAnswer,
puz.level,
puz.charactersGiven,
puz.charactersGiven
],
(tx, results) => {
console.log("Results", results.rowsAffected);
},
(err) => {
console.error(err);
}
);
}
});

我收到以下错误消息,我不知道这是什么意思,有人可以帮忙吗?

另外,有没有更好的方法来插入数组,而不是循环遍历每一行?

[16:07:09] 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": "db.db", }, "_running": true, "_txnQueue": Queue { "first": 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": 1, }, "version": "1.0", }, }

最佳答案

我不确定 SQLite 是否支持您的查询。

你可以尝试这样的事情:

INSERT INTO puzzles
(id, image, imageSolution, type, puzzleAnswer, level, charactersGiven, availableLetters)
SELECT ?,?,?,?,?,?,?,?
WHERE NOT EXISTS(SELECT 1 FROM puzzles WHERE id = ${puz.id})

关于javascript - react native SQLite 插入语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53469376/

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