简体   繁体   English

NodeJS sqlite3 SELECT查询缓存

[英]NodeJS sqlite3 SELECT query cached

I'm creating a discord bot (DiscordJS) using NodeJS and using sqlite3 to store 2 sets of data.我正在使用 NodeJS 创建一个不和谐机器人(DiscordJS)并使用 sqlite3 来存储 2 组数据。

I open the DB with:我打开数据库:

 let db = new sqlite3.Database('./db/games.db', (err) => {
        if (err) {
            console.error(err.message);
            return;
        }
        ...
}

Later on, I store a certain message in a table called promotedGames (id INCREMENTAL, server INT, channel INT, messageID INT)稍后,我将某条消息存储在名为促进游戏的表中(id INCREMENTAL,server INT,channel INT,messageID INT)

And when I query the db with当我查询数据库时

db.all(`SELECT * FROM promotedGames WHERE gameID = ${row.id}`, (err, promotedGamesResults) => {
...

It always returns the first ever game stored, I've no idea why this could be happening at all.它总是返回存储的第一个游戏,我完全不知道为什么会发生这种情况。

Steps taken so far: - Restarted the Node App - Deleted the sqlite3 database and redone it - Deleted the node_modules folder and reinstalled all the packages到目前为止采取的步骤: - 重新启动 Node 应用程序 - 删除 sqlite3 数据库并重做 - 删除 node_modules 文件夹并重新安装所有软件包

Any hints of what could be that I'm doing wrong?任何可能是我做错了什么的提示?

Thanks in advance提前致谢

您尝试使用 row.Id 搜索您的 gameID,sqlite 中的 row id 是您在表中插入的行的内置计数器,它不是列 gameID 。

I think you intend to return the records matching the ${row.id} It runs the query passed as an argument and for each result from the database, it will run the callback.我认为您打算返回与${row.id}匹配的记录它运行作为参数传递的查询,并且对于来自数据库的每个结果,它将运行回调。 Therefore is better to use db.each() when you intend to retrieve some items from the database.因此,当您打算从数据库中检索某些项目时,最好使用 db.each()。

db.each(`SELECT * FROM promotedGames WHERE gameID = ${row.id}`, (err, promotedGamesResults) => {

... ...

PS: db.each is used when you need to return all the rows and work on the selected returned data set. PS: db.each用于当您需要返回所有行并处理选定的返回数据集时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM