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
的值
const grab = sql.prepare(`SELECT bal FROM ${args[1]}`).get();
当这行代码运行时,它会从数据库中检索值并复制它。这意味着当数据库更新时,grab
的值不是。
如果要获取新余额,则需要再次查询数据库。