I have a problem running the following SQL query to my MySQL database with nodeJS and the node-mysql packe which is added with npm.
INSERT INTO locations (locationID,name,latitude,longitude)
SELECT '214777725','Bryans Road,
Maryland','38.628055556','-77.081666667'
FROM dual WHERE NOT EXISTS
(SELECT * FROM locations WHERE locationID = '214777725')
I've tried the following code:
var arrValues = [ -12.9747, -38.4767, 213287181, 'Salvador, Brazil' ];
var arrNames = [ 'latitude', 'longitude', 'locationID', 'name' ];
var locationID = "214777725";
if(imageData["locationID"] != null) {
var query = "INSERT INTO locations ? ";
query = query + " SELECT (?)";
query = query + " FROM dual WHERE NOT EXISTS";
query = query + " (SELECT * FROM locations WHERE locationID = ?)";
console.log(query);
connection.query(query, [arrNames,arrValues,locationID], function(err,res)
{
if(err)
{
console.log(err);
}
else
{
console.log("Insert-Success: " +id);
}
});
}
Running this the console writes following error:
{
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0
}
I hope somebody can help.
It looks like you need to use the .toString(); function on your arrays.
connection.query(query, [arrNames.toString(),arrValues.toString(),locationID], function(err,res)
{ ...