MySQL 与 Node.js

标签 mysql node.js

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

如何将 MySQL 与 Node.js 一起使用?

最佳答案

查看 node.js module list

  • > node-mysql — 一个实现 MySQL 协议(protocol)的 node.js 模块
  • > node-mysql2 — 另一个纯 JS 异步驱动程序。流水线、准备好的语句。
  • > node-mysql-libmysqlclient — 基于 libmysqlclient 的 MySQL 异步绑定(bind)

node-mysql 看起来很简单:

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'

关于MySQL 与 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636585/

相关文章:

重启后 Mysql (MariaDB) 访问被拒绝

javascript - 用于 Rocket Chat iframe 身份验证的 NodeJS 应用程序抛出 SyntaxError : Unexpected token (

javascript - 如何将参数传递给 express.js 路由器?

javascript - 使用 Express 查询 MongoDB 服务器(Node.js 项目)

node.js - POST 请求中的输入无效

mysql - SQL GROUP_CONCAT 不适用于 2 表连接

php - 创建复杂的 hasManyThrough Laravel 4 查询

mysql - Insert table into another table conditionally - insert into select 案例

java - 如何使用 hibernate 和 MySQL 在 java 中创建单页应用程序 (SPA)

node.js - Node : middleware should remove itself from the stack