【问题标题】:discord.js better-sqlite3 addition is not actually adding the result from a previous constdiscord.js better-sqlite3 添加实际上并没有添加先前 const 的结果
【发布时间】:2020-01-20 03:37:20
【问题描述】:
if (command == "tgive") {
      //Get their current balance
    const grab = sql.prepare(`SELECT bal FROM ${args[1]}`).get();
    //Grab the value from the first input after the second. Ex: eco tgive 5 Juliana
    const pointsToAdd = parseInt(args[0]);
    //Add the two values from the database and the args[0] input
    const result = +grab.bal + +pointsToAdd;
    //Replace the curret value from column bal in table ${args[1]}, with the const result
    sql.prepare(`REPLACE INTO ${args[1]} (bal) VALUES ('${result}');`).run();
    message.channel.send(`You have ${grab.bal}`);

}
});

在数据库“Juliana”中,bal 列的值为420,但是,每当我使用5 的值运行此命令时,我得到的是You have 420,而不是You have 425,意味着该命令没有添加来自const result的值

【问题讨论】:

  • 啊,还有一个注意事项。我现在才注意到这一点:Sqlite3 数据库正在添加另一个具有不同值的集合,因此它在技术上添加了输入的值,但是,它添加了更多行。所以从技术上讲,我在 DB 浏览器中得到了类似的东西:balFilter1 [420]2 [5567]3 [425]

标签: node.js discord.js better-sqlite3


【解决方案1】:
const grab = sql.prepare(`SELECT bal FROM ${args[1]}`).get();

当这行代码运行时,它会从数据库中检索值并对其进行副本。这意味着当数据库更新时,grab 的值不会。

如果你想获得新的余额,那么你需要再次查询数据库。

【讨论】:

  • 啊,我输入了sql.prepare(`UPDATE ${args[1]} SET bal = ${result} WHERE rowid = 1;`).run();,好像可以了,谢谢!
猜你喜欢
  • 2020-08-10
  • 2022-01-03
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多