[英]node.js and mysql how do I get 2 if have 1 in the database?
I want to add + 1 to the database and with that I would have 2 in the database if I had 1 in the case, I tried it but got 11, not 2 我想向数据库中添加+1,如果我有1,我将在数据库中有2,我尝试过但得到11,而不是2
if (result4.length) {
var sql1 = `UPDATE level SET xp = '${result4[0].xp + 1}' WHERE xp = '${result4[0].xp}'`;
connection.query(sql1, function(err, result) {
if (err) throw err;
});
}
Here is the full source, just in case: 这是完整的源代码,以防万一:
client.on("message", message => {
if (message.author.bot) return;
connection.query(`SELECT * FROM guildn WHERE id = '${message.guild.id}'`, function(err, result3) {
if (err) {
return console.log('Error1');
}
if (result3.length) {
connection.query(`SELECT * FROM level WHERE id = '${message.author.id}'`, function(err, result4) {
if (err) {
return console.log('Error1');
}
if (!result4.length) {
var sql = `INSERT INTO level (guild , id , nivel , xp) VALUES ('${message.guild.id}','${message.author.id}','0','1')`;
connection.query(sql, function(err, result) {
if (err) throw err;
console.log("1 record inserted");
return
});
}
if (result4.length) {
var sql1 = `UPDATE level SET xp = '${result4[0].xp + 1}' WHERE xp = '${result4[0].xp}'`;
connection.query(sql1, function(err, result) {
if (err) throw err;
});
}
});
}
});
});
I used the google translator to make this post, so if I have any errors in the translation, I'm sorry 我曾用Google翻译器发表这篇文章,所以如果翻译中有任何错误,对不起
My suspicion would be that the result from the database xp
is a string. 我怀疑是数据库
xp
的结果是一个字符串。 This seems logical since you are setting it as a string in your query. 这似乎合乎逻辑,因为您在查询中将其设置为字符串。 In that case you would do this:
在这种情况下,您可以这样做:
`UPDATE level SET xp = '${parseInt(result4[0].xp) + 1}' WHERE xp = '${result4[0].xp}'`;
This converts the result to a number and javascript will add 1 to it, rather than concatenating 1 to the string. 这会将结果转换为数字,而javascript会将数字加1,而不是将1连接到字符串。
I'm guessing what's happening is that your XP data is stored in your database as a string so you end up trying to add a number to a string. 我猜发生了什么事,您的XP数据以字符串形式存储在数据库中,因此您最终尝试在字符串中添加数字。 In Javascript, the result in that situation is that you concatenate the number to the end of the string, which is why you end up with '11' instead of 2.
在Javascript中,这种情况的结果是将数字连接到字符串的末尾,这就是为什么以'11'而不是2结尾的原因。
To resolve the issue without changing the database, you can turn the XP string into an integer using parseInt before adding 1 to it: 要在不更改数据库的情况下解决此问题,可以在添加1之前使用parseInt将XP字符串转换为整数:
var sql1 = `UPDATE level SET xp = '${ parseInt (result4[0].xp) + 1}' WHERE xp = '${result4[0].xp}'`;
If the data type of xp
is a string then a little modification to your update call would suffice: 如果
xp
的数据类型是字符串,那么对您的更新调用进行一些修改就足够了:
if (result4.length) {
const query = `UPDATE level SET xp = '${Number(result4[0].xp) + 1}' WHERE xp = '${result4[0].xp}'`;
connection.query(query, function(err, result) {
if (err) throw err;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.