我刚刚开始接触Node.js。我来自PHP背景,因此我习惯于使用MySQL满足所有数据库需求。
如何在Node.js中使用MySQL?
查看node.js模块列表
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'