Home >Web Front-end >JS Tutorial >What are the methods for nodeJs to connect to mysql?

What are the methods for nodeJs to connect to mysql?

php中世界最好的语言
php中世界最好的语言Original
2018-04-12 16:56:321530browse

This time I will bring you nodeJs connectionmysqlWhat are the methods, what are the notesfor connecting nodeJs to mysql, the following is a practical case, let's take a look.

mysql.js

//连接数据库
var mysql = require('mysql');
var pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: 'gfdfsdf34r34r',
  database:'caiji'
});
module.exports = function(sql, callback) {
  pool.getConnection(function(conn_err, conn) {
    if(conn_err) {
      callback(err,null,null);
    } else {
      conn.query(sql, function(query_err, rows, fields) {
        conn.release();
        callback(query_err, rows, fields);
      });
    }
  });
};

Call

var query = require("./mysql.js");
query(sql, function(err, rows, fields) {
  console.log(rows);
});

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to achieve the undo and redo effect in Immutable.js

easyui date and time box in IE How to deal with compatibility

The above is the detailed content of What are the methods for nodeJs to connect to mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn