码迷,mamicode.com
首页 > 数据库 > 详细

node.js之mysql包使用说明

时间:2014-11-16 20:12:59      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   color   ar   os   使用   sp   strong   

  作者:zhanhailiang 日期:2014.11.16

本文将讲解如何在node.js平台上依赖mysql包实现对mysql的访问。

1. 安装:

[root@~/wade/nodejs/nodebeginner/mysql_test]# npm install mysql

2. 基于mysql包实现对mysql的连接和查询:

[root@~/wade/nodejs/nodebeginner/mysql_test]# cat mysqltest.js 
var mysql      = require(‘mysql‘);
var connection = mysql.createConnection({
    host     : ‘localhost‘,
    user     : ‘******‘,
    password : ‘******‘
});
 
connection.connect();
 
connection.query(‘SELECT * FROM test.test‘, function(err, rows, fields) {
    if (err) {
        throw err;
    }
 
    console.log(rows);
    console.log(fields);
});
 
connection.end(); // 注册end处理器,不用担心query异步没完成就connection close

3. 实现步骤:

其和其它语言并无区别——先配置参数说明连接要连接的DB,用什么方式连接,用哪个账号连接,连接成功后执行相应的sql操作,最后关闭连接。

4. 完整代码:

https://github.com/billfeller/nodebeginner/tree/master/mysql_test

5. 官方文档:

https://www.npmjs.org/package/mysql

node.js之mysql包使用说明

标签:style   http   io   color   ar   os   使用   sp   strong   

原文地址:http://blog.csdn.net/billfeller/article/details/41175081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!