javascript - mySql 插入语法 - 不插入预期的内容
问题描述
我有这个 INSERT 查询。它确实在表中创建了一条记录,但“client”值为 0,“short”值为 null。但是,所有控制台日志都显示正确的数据。我假设这是我使用问号时的简单语法错误。但我找不到它,或者似乎也只能使用变量名让它工作。任何帮助表示赞赏。
app.put("/audit/create", function (req, res) {
console.log("It is getting to the route");
const client = req.body.client;
const short = req.body.short;
console.log(`client: ${client}`);
console.log(`short: ${short}`);
connection.getConnection(function (err, connection) {
connection.query(
`INSERT INTO audits (client, short)
VALUES (?, ?)`,
[
{
client: client,
},
{
short: short,
},
],
function (error, results, fields) {
if (error) throw error;
res.json(results);
console.log(`Audit has been created`);
}
);
connection.release();
});
});
我还尝试使用 ${client}、${short} 的变量名称代替问号......这仍然给了我相同的结果。但是,呈现 ${client} 和 ${short} 的控制台日志确实记录了我期望看到的正确数据。
解决方案
为什么将对象作为值传递给查询?试试:
connection.query(
'INSERT INTO audits (client, short) VALUES (?, ?)',
[ client, short ],
function ...
推荐阅读
- yaml - terraform kubernetes 提供程序的多行字符串注释
- javascript - 如何导入基于承诺的变量以在另一个模块(javascript)中使用?
- django - 在 django 中传递参数
- jquery - 使用父选择器上的通配符从子代获取 JQuery 更改事件
- javascript - Node Express.js 从 json 数据中删除一个条目
- java - 如何在 jaxb 中使毫秒可选?
- android - mpandroidchart - 无法正确显示 x 轴标签
- android - Kotlin单元测试assertEquals不起作用
- jquery - 如何选择没有特定祖先元素的元素
- python - 如何在函数中创建字典?