【发布时间】:2022-01-13 13:00:47
【问题描述】:
我正在尝试使用以下查询将一些测试数据发布到 MYsql 数据库中:
Asset.create = (newAsset, result) => {
sql.query("INSERT INTO `assets` VALUES (?, ?, ?, ?, ?)", newAsset, (err, res) => {
if (err) {
console.log("error", err);
result(err, null);
return;
}
console.log("Created new asset : ", { id: res.insertId, ...newAsset});
result(null, {id: res.insertId, ...newAsset});
});
在邮递员中使用以下 Json 输入:
{
"Id" : 6,
"AccountID" : "6",
"AssetID" : "6",
"AssetSymbol" : "6",
"Amount" : "60"
}
但是我得到以下错误:
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?)' at line 1",
sql: "INSERT INTO `assets` VALUES (`id` = 6, `accountid` = '6', `assetid` = '6', `assetsymbol` = '6', `amount` = '60', ?, ?, ?, ?)"
我的 assets 表的 MySQL 模型如下:
Id: INT (Not autoincrementing)
AccountID: VARCHAR(45)
AssetID: VARCHAR(45)
AssetSymbol: VARCHAR(45)
Amount: VARCHAR(45)
我知道它对于 MySQL 数据库的设计很糟糕。它现在只是一个测试数据库,直到我掌握了将 MySql 与 express 一起使用的窍门,并且稍后会制作一个新的合适的数据库。 任何关于我做错了什么的建议将不胜感激。
【问题讨论】:
-
似乎您混淆/混合了至少 3 种不同的 INSERT 编写方法。有Documentation about MySQL and the INSERT specifically也许你应该快速回顾一下
-
我认为你可以这样做,从它扩展到
sql.query("INSERT INTO `assets` SET ?, ?, ?, ?, ?", newAsset .....来判断 -
或者使用您所传递的参数
[6,6,6,60]中的值 -
我确实怀疑我混淆了不同的方法。我有一种方法不起作用,所以我跳到 Stack 上寻找其他插入 mysql 的方法。它不适合我的其他工作,所以我尽可能地让它适合
-
尽管如此,下面有人找到了使其工作的方法。谢谢各位朋友的建议。它的赞赏
标签: mysql database express postman