具有节点的复杂 SQL 查询 - mysql
Complex SQL query with nodes-mysql
我有一个问题 运行 以下 SQL 查询我的 MySQL 数据库与 nodeJS 和 node-mysql 包是用 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')
我试过以下代码:
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);
}
});
}
运行 控制台显示以下错误:
{
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0
}
希望有人能帮忙。
看来您需要使用 .toString();在你的数组上运行。
connection.query(query, [arrNames.toString(),arrValues.toString(),locationID], function(err,res)
{ ...
我有一个问题 运行 以下 SQL 查询我的 MySQL 数据库与 nodeJS 和 node-mysql 包是用 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')
我试过以下代码:
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);
}
});
}
运行 控制台显示以下错误:
{
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0
}
希望有人能帮忙。
看来您需要使用 .toString();在你的数组上运行。
connection.query(query, [arrNames.toString(),arrValues.toString(),locationID], function(err,res)
{ ...