使用Nodejs在MySQL中大容量插入只插入一行
我正在尝试进行批量插入,甚至遵循了此处的代码: 它只插入第一行,不知道为什么?使用nodejs和mysql使用Nodejs在MySQL中大容量插入只插入一行,mysql,node.js,Mysql,Node.js,我正在尝试进行批量插入,甚至遵循了此处的代码: 它只插入第一行,不知道为什么?使用nodejs和mysql var ethnicity_interested = [1, 2, 3]; var newEthArr = []; ethnicity_interested.forEach(function(i) { newEthArr.push([i, userObj.id]) })
var ethnicity_interested = [1, 2, 3];
var newEthArr = [];
ethnicity_interested.forEach(function(i) {
newEthArr.push([i, userObj.id])
})
var fields = "INSERT INTO User_Pref_Ethnicity (ethnicity_id, user_id) VALUES (?)";
var inserts = [
newEthArr
];
var sqlNewUser = mysql.format(fields, newEthArr);
connection.query(sqlNewUser, function(
error,
results,
fields
) {
if (error) {
console.log("insert User_Pref_Ethnicity err: " + error);
res.json({
http_code: 500,
error_message: 'Server Error: Please Try Again.'
});
} else {
storeUserPrefReligion();
}
});
我甚至尝试使用示例中的版本,如下所示:
var fields = "INSERT INTO User_Pref_Ethnicity (ethnicity_id, user_id) VALUES ?";
var inserts = [
newEthArr
];
///var sqlNewUser = mysql.format(fields, newEthArr);
connection.query(fields, newEthArr, function(
我得到了这个错误:
insert User_Pref_Ethnicity err: Error: ER_PARSE_ERROR: 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 '2, 2' at line 1
您所说的示例没有使用
mysql.format()
。它说使用值?
而不是值(?)
。如果您更精确地遵循示例,会发生什么?我删除了括号,然后删除了格式,现在我得到了以下错误:insert User\u Pref\u error:error:ER\u PARSE\u error:SQL语法中有一个错误;检查与MySQL服务器版本对应的手册,以了解第1行“2,2”附近使用的正确语法