gpt4 book ai didi

MySQL 查询不会提取 Node.js 中的值

转载 作者:行者123 更新时间:2023-11-29 10:34:14 25 4
gpt4 key购买 nike

我很困惑,这是我第一次同时使用 Node 和 MySQL。我已经设法让两者都工作。但是,我的查询在执行时似乎没有使用存储在变量中的值。无论命令是什么,它每次都会输入空值。在本例中,我收到一个字符串并将数组拆分为命令。数组最多有 3 个。我只使用查询中的第一个值,在这个测试场景中它是 A。代码如下:

console.log('Attempting to Create fireteam');
var ftcmds = suffix.split(" ", 3);
console.log(ftcmds);
var Name = ftcmds[0];
console.log('name is '+ Name);
var ActivityDate = moment();
console.log('the date is '+ActivityDate)
var Owner = msg.author;
console.log('the owner is '+ Owner);

var ft = [Owner.id];
console.log('the ft is '+ ft);
var FireTeam = JSON.stringify(ft);
console.log('the stringified object is '+ FireTeam);

var sql = "INSERT INTO fireteam (Name, Owner, FireTeam, ActivityDate) VALUES (Name, Owner, FireTeam, ActivityDate)"
//ftArr.push(ft);
console.log("Attempting to run query "+ sql + "on conncetion" + mysqlcon);
mysqlcon.query(sql, function (err, result) {
console.log("Result |" +result);
console.log("Error |" +err);
    if (err) throw err;

console.log("1 record inserted");
msg.author.send("Created Fireteam ", name, ". I will setup a group chat on ", date, " if your team fills up. Go recruit guardians!");
 
});

当我记录变量时,我可以看到它们正在使用正确的值进行更新,因此我很困惑为什么查询会插入所有空值。

最佳答案

您应该在查询中连接这些变量

所以

    var sql = "INSERT INTO fireteam (Name, Owner, FireTeam, ActivityDate) VALUES (Name, Owner, FireTeam, ActivityDate)"

实际上应该是

    var sql = "INSERT INTO fireteam (Name, Owner, FireTeam, ActivityDate) VALUES ('" + Name + "','" + Owner + "','" + FireTeam + "','" + ActivityDate + "')"

关于MySQL 查询不会提取 Node.js 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46782677/

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