无法使用mysql模块在node.js中执行Sql查询

时间:2022-09-04 12:08:26

Below is my app.js file content. When i try to execute this in node i am getting "Error while performing Query." Although this table contains 5 rows but still something seems to be wrong. Please let me know where i am doing wrong

以下是我的app.js文件内容。当我尝试在节点中执行此操作时,我收到“执行查询时出错”。虽然这个表包含5行,但似乎仍然有些错误。请让我知道我在哪里做错了

app.js

var express = require('express');
var mysql = require('mysql');
var app = express();
var port = process.env.PORT || 8081;

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "mydata"
});

con.connect(function(err){
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

app.get("/",function(req,res){
  con.query("select * from club_data ", function(err, rows,field) {
  if (!err)
  {
    console.log('The solution is: ', rows);
   }
  else
    console.log('Error while performing Query.');
  });
});



con.end(function(err) {
  // The connection is terminated gracefully
  // Ensures all previously enqueued queries are still
  // before sending a COM_QUIT packet to the MySQL server.
});

// START THE SERVER
// =============================================================================
app.listen(port);

Server Output:

PS C:\Users\skull_king\Desktop\Workspace> node .\app.js
Connection established
Error while performing Query.

1 个解决方案

#1


0  

Remember that Node.JS is mostly asynchronous. The request handler that you declare with app.get(...) is not executed immediately, but only later, when the first (or any) request is received.

请记住,Node.JS主要是异步的。使用app.get(...)声明的请求处理程序不会立即执行,而是仅在稍后,当收到第一个(或任何)请求时执行。

Immediately after declaring the request handler, you're closing the MySQL connection with the con.end(...) call even before the first request is handled. When you're trying to handle the request, con is already closed, because you have closed it with con.end before.

在声明请求处理程序之后,即使在处理第一个请求之前,您也正在使用con.end(...)调用关闭MySQL连接。当您尝试处理请求时,con已经关闭,因为您之前已经使用con.end关闭它。

What you are probably trying to achieve is to close the MySQL connection when the server shuts down. For that, you could listen on the server's close event (untested, though):

您可能尝试实现的是在服务器关闭时关闭MySQL连接。为此,您可以监听服务器的关闭事件(尽管未经测试):

app.on('close', function() {
    con.end();
});

#1


0  

Remember that Node.JS is mostly asynchronous. The request handler that you declare with app.get(...) is not executed immediately, but only later, when the first (or any) request is received.

请记住,Node.JS主要是异步的。使用app.get(...)声明的请求处理程序不会立即执行,而是仅在稍后,当收到第一个(或任何)请求时执行。

Immediately after declaring the request handler, you're closing the MySQL connection with the con.end(...) call even before the first request is handled. When you're trying to handle the request, con is already closed, because you have closed it with con.end before.

在声明请求处理程序之后,即使在处理第一个请求之前,您也正在使用con.end(...)调用关闭MySQL连接。当您尝试处理请求时,con已经关闭,因为您之前已经使用con.end关闭它。

What you are probably trying to achieve is to close the MySQL connection when the server shuts down. For that, you could listen on the server's close event (untested, though):

您可能尝试实现的是在服务器关闭时关闭MySQL连接。为此,您可以监听服务器的关闭事件(尽管未经测试):

app.on('close', function() {
    con.end();
});