MySQL 与 Node.js [英] MySQL with Node.js

查看:63
本文介绍了MySQL 与 Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始接触 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.

如何在 Node.js 中使用 MySQL?

How can I use MySQL with Node.js?

推荐答案

查看 node.js 模块列表

  • 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'

这篇关于MySQL 与 Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆