简体   繁体   中英

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

Node.js Code:

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. But when I run this code using node.js, I am unable to get the output as it throws the error. The version for SQL is 0.9 in node when I download the node_modules.

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. 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.

Also, you cannot define a string in the way that you have in your query method. 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. 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]);

  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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