I'm stuck with inserting JSON data to MySQL db using NodeJS. I got this error:
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 ''[{\"id\":\"d3f3d\",\"code\":\"' at line 1
I'm getting JSON data from url.json using the request Node module and I'm trying to store the data in a MySQL db.
//mysql connection setup
var connection = mysql.createConnection({
host : "localhost",
port: "3306",
user : "root",
password : "root",
database : "db",
multipleStatements: true
});
request('url.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
//console.log(body)
}
var sql = "INSERT INTO table (id, code, country_name, city) VALUES ?";
var data = JSON.parse(body);
var responseJson = JSON.stringify(data.response.docs);
var query = connection.query(sql, [responseJson], function(err, result) {
if(err) throw err;
console.log('data inserted');
});
console.log(query.sql);
});
The data is logged as '' '[{\"id\":\"d3f3d\",\"code\":\"'... }]'. I think this may be the source of the error.
JSON structure looks like this:
{
"header":
{
"file1":0,
"file2":1,
"subfiles":{
"subfile1":"true",
"subfile2":"true",
}
},
"response":
{
"number":678,
"start":0,
"docs":[
{
"id":"d3f3d",
"code":"l876s",
"country_name":"United States",
"city":"LA"
},
{
"id":"d2f2d",
"code":"2343g",
"country_name":"UK",
"city":"London"
}
]
}
}
How do I resolve this?
Thanks.