node.js mySQL 发送带有 INSERT 语句格式的数组
node.js mySQL sending an array with INSERT statement formatting
我在数组中有以下内容,打印自:
console.log([userData]) //From HTML Form
-------------------------------
[ [ 'test3',
'Alice',
'Weather',
'Bob',
'Doe',
'john.doe@gmail.com',
'Footballer',
'male',
'english',
'married',
'1965-01-02' ] ]
现在我需要将其插入 mySQL 数据库,结果是一个语法 error.The 我似乎不明白的是如何以 mySQL可以'understand'
var sql = "INSERT INTO users (uid,FirstName,LastName,MotherName,FatherName,Email,Profession,Gender,MaritalStatus,Birthday) VALUES ?";
con.query(sql, "'" + userData.join() + "'", function(err) {
if (err) throw err;
console.log("inserted")
});
输出:
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
''\'test3,Alice,Weather,Bob,Doe,john.doe@gmail.com,Footballer,male,english,married,1965-01-02'
at line 1
谢谢。
你不应该加入 userData,因为一旦加入,它就会变成一个完整的字符串,mysql 库会替换成 ?
。整个查询分解如下:
INSERT INTO users (uid,FirstName,LastName,MotherName,FatherName,Email,Profession,Gender,MaritalStatus,Birthday) VALUES "'test3,Alice,Weather,Bob,Doe,john.doe@gmail.com,Footballer,male,english,married,1965-01-02'"
这绝对是错误的SQL。
要正确插入值,您必须按原样将值传递给 mysql 库:
con.query(sql, [[userData]], function(err) {
if (err) throw err;
console.log("inserted")
});
第二个参数应该是"triple array",即[[['test3', 'alice'..... ]]].
其中第一个数组表示您正在为mysql库传递一系列参数来进行替换,第二个数组包含参数的值(即要替换为?
),第三个数组告诉 mysql 库这个参数是一个值数组。
我在数组中有以下内容,打印自:
console.log([userData]) //From HTML Form
-------------------------------
[ [ 'test3',
'Alice',
'Weather',
'Bob',
'Doe',
'john.doe@gmail.com',
'Footballer',
'male',
'english',
'married',
'1965-01-02' ] ]
现在我需要将其插入 mySQL 数据库,结果是一个语法 error.The 我似乎不明白的是如何以 mySQL可以'understand'
var sql = "INSERT INTO users (uid,FirstName,LastName,MotherName,FatherName,Email,Profession,Gender,MaritalStatus,Birthday) VALUES ?";
con.query(sql, "'" + userData.join() + "'", function(err) {
if (err) throw err;
console.log("inserted")
});
输出:
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 ''\'test3,Alice,Weather,Bob,Doe,john.doe@gmail.com,Footballer,male,english,married,1965-01-02' at line 1
谢谢。
你不应该加入 userData,因为一旦加入,它就会变成一个完整的字符串,mysql 库会替换成 ?
。整个查询分解如下:
INSERT INTO users (uid,FirstName,LastName,MotherName,FatherName,Email,Profession,Gender,MaritalStatus,Birthday) VALUES "'test3,Alice,Weather,Bob,Doe,john.doe@gmail.com,Footballer,male,english,married,1965-01-02'"
这绝对是错误的SQL。
要正确插入值,您必须按原样将值传递给 mysql 库:
con.query(sql, [[userData]], function(err) {
if (err) throw err;
console.log("inserted")
});
第二个参数应该是"triple array",即[[['test3', 'alice'..... ]]].
其中第一个数组表示您正在为mysql库传递一系列参数来进行替换,第二个数组包含参数的值(即要替换为?
),第三个数组告诉 mysql 库这个参数是一个值数组。