带有 Node.js 的 MySQL

2021-11-20 00:00:00 node.js mysql

我刚刚开始接触 Node.js.我来自 PHP 背景,所以我相当习惯使用 MySQL 来满足我所有的数据库需求.

I've just started getting into Node.js. I come from a PHP background, so I'm fairly used to using MySQL for all my database needs.

如何将 MySQL 与 Node.js 结合使用?

How can I use MySQL with Node.js?

推荐答案

查看 node.js 模块列表

  • node-mysql — 一个实现 MySQL 协议的 node.js 模块
  • node-mysql2 — 另一个纯 JS 异步驱动程序.流水线,准备好的语句.
  • node-mysql-libmysqlclient — 基于 libmysqlclient 的 MySQL 异步绑定
  • node-mysql — A node.js module implementing the MySQL protocol
  • node-mysql2 — Yet another pure JS async driver. Pipelining, prepared statements.
  • node-mysql-libmysqlclient — MySQL asynchronous bindings based on libmysqlclient

node-mysql 看起来很简单:

node-mysql looks simple enough:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret',
});

connection.connect(function(err) {
  // connected! (unless `err` is set)
});

查询:

var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
  // Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

相关文章