简体   繁体   English

无法使用Node.js中的多个用户定义变量运行MYSQL查询

[英]Unable to run MYSQL query using multiple user-defined variables from Node.js

Node.js Code: Node.js代码:

var express    = require("express");
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'password',
  database : 'myDatabase'
});
var app = express();

connection.connect(function(err){
if(!err) {
    console.log("Database is connected ... \n\n");
} else {
    console.log("Error connecting database ... \n\n");
}
});

app.get("/",function(req,res){
var tmdb_id, rel_date, role, title, year, starring, actor, categories,choose,use;

choose = 3;
if (choose == 0 || choose == 1 || choose ==2 || choose==3){
use = categories[choose];
connection.query('
SET @tmdbid:= (SELECT TMDB_ID FROM `MOVIES` WHERE VOTES > 2058 ORDER BY RAND() LIMIT 0,1);
SET @genre:= (SELECT GROUP_CONCAT(GENRE_NAME) FROM `MOVIES_GENRES` WHERE TMDB_ID = @tmdbid);
SET @director:=(SELECT GROUP_CONCAT(DISTINCT NAME) FROM`DIRECTORS` WHERE DID IN (SELECT DID FROM `DIRECTS` WHERE TMDB_ID = @tmdbid));
SET @actors:=(SELECT GROUP_CONCAT(DISTINCT NAME) FROM `ACTORS` WHERE AID IN (SELECT AID FROM `ROLES` WHERE TMDB_ID = @tmdbid));
SET @roles:=(SELECT GROUP_CONCAT(DISTINCT CHAR_NAME) FROM `ROLES` WHERE TMDB_ID = @tmdbid);
SET @plot:= (SELECT OVERVIEW FROM `OVERVIEW` WHERE TMDB_ID = @tmdbid);
SELECT TITLE, RELEASE_DATE, @genre, @director, @actors, @roles, @plot
FROM `MOVIES` WHERE TMDB_ID = @tmdbid', function(err,rows,fields){
  console.log(err);
  if (!err){
    console.log(rows[0]);
  }

 else
    console.log('Error while performing Query.');
  });
}


});
app.listen(3000);

Query: 查询:

SET @tmdbid:= (SELECT TMDB_ID FROM `MOVIES` WHERE VOTES > 2058 ORDER BY RAND() LIMIT 0,1);
SET @genre:= (SELECT GROUP_CONCAT(GENRE_NAME) FROM `MOVIES_GENRES` WHERE TMDB_ID = @tmdbid);
SET @director:=(SELECT GROUP_CONCAT(DISTINCT NAME) FROM`DIRECTORS` WHERE DID IN (SELECT DID FROM `DIRECTS` WHERE TMDB_ID = @tmdbid));
SET @actors:=(SELECT GROUP_CONCAT(DISTINCT NAME) FROM `ACTORS` WHERE AID IN (SELECT AID FROM `ROLES` WHERE TMDB_ID = @tmdbid));
SET @roles:=(SELECT GROUP_CONCAT(DISTINCT CHAR_NAME) FROM `ROLES` WHERE TMDB_ID = @tmdbid);
SET @plot:= (SELECT OVERVIEW FROM `OVERVIEW` WHERE TMDB_ID = @tmdbid);
SELECT TITLE, RELEASE_DATE, @genre, @director, @actors, @roles, @plot
FROM `MOVIES` WHERE TMDB_ID = @tmdbid

I am trying to save all user defined variable values and then displaying it at the end. 我试图保存所有用户定义的变量值,然后在最后显示它。 This code works well in MYSQL 5.7 using the MYSQL workbench 6.3CE. 此代码在使用MYSQL工作台6.3CE的MYSQL 5.7中运行良好。 But when I run this code using node.js, I am unable to get the output as it throws the error. 但是,当我使用node.js运行此代码时,由于抛出错误,因此无法获得输出。 The version for SQL is 0.9 in node when I download the node_modules. 当我下载node_modules时,节点中SQL的版本为0.9。

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 'SELECT @tmdbid' at line 1]
  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlState: '42000',
  index: 0 }

I am not sure where I am going wrong here. 我不确定我要去哪里错了。

According to the node-mysql documentation there is no default support for multiple sql statements in one query. 根据node-mysql文档 ,在一个查询中不默认支持多个sql语句。 Perhaps this is why you're having problems. 也许这就是为什么您遇到问题。

Turn on support for multiple statements when setting up the initial connection: 设置初始连接时打开对多条语句的支持:

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'password',
  database : 'myDatabase',
  multipleStatements: true /* turn on multiple statements */
});

Do note that this could increase the risk of SQL injection attacks, as per the aforementioned documentation. 请注意 ,根据上述文档,这可能会增加SQL注入攻击的风险。

Also, you cannot define a string in the way that you have in your query method. 另外,您无法以query方法中的方式定义字符串。 It seems you've formatted the string with carriage returns. 看来您已经用回车符格式化了字符串。 It's surprising that you didn't get a syntax error from this before the sql error. 令人惊讶的是,您没有在sql错误之前从中得到语法错误。 Perhaps the code you've pasted here is not verbatim? 也许您粘贴在此处的代码不是逐字记录的?

Anyway, I would keep the whole query on one line to be safe. 无论如何,为了安全起见,我会将整个查询放在一行上。 So your query would look like this: 因此,您的查询将如下所示:

connection.query('SET @tmdbid:= (SELECT TMDB_ID FROM `MOVIES` WHERE VOTES > 2058 ORDER BY RAND() LIMIT 0,1); SET @genre:= (SELECT GROUP_CONCAT(GENRE_NAME) FROM `MOVIES_GENRES` WHERE TMDB_ID = @tmdbid); SET @director:=(SELECT GROUP_CONCAT(DISTINCT NAME) FROM `DIRECTORS` WHERE DID IN (SELECT DID FROM `DIRECTS` WHERE TMDB_ID = @tmdbid)); SET @actors:=(SELECT GROUP_CONCAT(DISTINCT NAME) FROM `ACTORS` WHERE AID IN (SELECT AID FROM `ROLES` WHERE TMDB_ID = @tmdbid)); SET @roles:=(SELECT GROUP_CONCAT(DISTINCT CHAR_NAME) FROM `ROLES` WHERE TMDB_ID = @tmdbid); SET @plot:= (SELECT OVERVIEW FROM `OVERVIEW` WHERE TMDB_ID = @tmdbid); SELECT TITLE, RELEASE_DATE, @genre, @director, @actors, @roles, @plot FROM `MOVIES` WHERE TMDB_ID = @tmdbid',

  function (err, rows, fields) {

    if (err)
      console.log(err);
    else
      console.log(rows[0]);

  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM