nodejs使用MySQL
来源:互联网 发布:炼数成金 大数据分析师 编辑:程序博客网 时间:2024/06/06 17:07
一、搭建环境
nodejs和MySQL的安装就不说了,安装好后,通过npm安装MySQL模块。
在自己nodejs项目的根目录,运行npm install mysql --save
其中,--save
是将mysql模块写入package.json文件的依赖模块配置中,也可以不添加此选项。
二、创建数据库
运行下面的sql语句,可创建一个简单的数据库和user表
创建数据库tsnode
1
CREATE
DATABASE
`tsnode`;
创建表user
1
2
3
4
5
6
7
8
9
10
CREATE
TABLE
`
user
` (
`userid`
int
(11)
NOT
NULL
AUTO_INCREMENT,
`username`
varchar
(45)
DEFAULT
NULL
COMMENT
'账号'
,
`
password
`
varchar
(45)
DEFAULT
NULL
COMMENT
'密码'
,
`userdesc`
varchar
(145)
DEFAULT
NULL
COMMENT
'备注'
,
`birthday` datetime
DEFAULT
CURRENT_TIMESTAMP
COMMENT
'生日'
,
`logincount`
int
(11)
DEFAULT
'0'
COMMENT
'登录次数'
,
PRIMARY
KEY
(`userid`),
UNIQUE
KEY
`iduser_UNIQUE` (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=6
DEFAULT
CHARSET=utf8 COMMENT=
'table for user manage'
;
三、项目js文件中引入mysql模块并操纵数据库
1、连接数据库并查询,将返回数据从控制台输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var
mysql = require(
'mysql'
);
var
connection = mysql.createConnection({
'host'
:
'localhost'
,
'user'
:
'root'
,
'password'
:
'passoo'
,
'port'
:
'3307'
,
'database'
:
'tsnode'
});
connection.connect();
connection.query(
'select * from user limit 0,10'
,
function
(err, rows, fields){
if
(err)
throw
err;
console.log(rows);
// 输出查询结果JSON
var
birthday = rows[0].birthday;
console.log(birthday.getTime());
// 输出生日
console.log(fields);
// 输出字段属性
});
connection.end();
2、使用连接池
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 使用连接池
*/
var
mysql = require(
'mysql'
);
var
pool = mysql.createPool({
'host'
:
'localhost'
,
'user'
:
'root'
,
'password'
:
'passoo'
,
'port'
:
'3307'
,
'database'
:
'tsnode'
});
pool.getConnection(
function
(err, connection){
console.log(
'1 connected!'
);
// 执行SQL语句
connection.query(
"INSERT INTO `tsnode`.`user` (`username`, `password`, `userdesc`) VALUES ('realwall', 'passoo', 'This is user desc2')"
,
function
(err, result){
if
(err){
connection.rollback(
function
(){
throw
err;
});
}
console.log(
'connection 1 insert'
);
console.log(result);
// 将连接释放到连接池
connection.release();
console.log(
'connection 1 released'
);
});
});
pool.getConnection(
function
(err, connection){
if
(err)
throw
err;
console.log(
'2 connected!'
);
// 执行SQL语句
connection.query(
'select * from user limit 0,10'
,
function
(err, rows){
console.log(
'connection 2 query'
);
console.log(rows);
// 将连接释放到连接池
connection.release();
console.log(
'connection 2 released'
);
});
});
0 0
- Nodejs使用mysql
- nodejs使用MySQL
- nodejs使用mysql例子
- Nodejs使用mysql
- nodejs连接mysql及使用
- nodejs中mysql的使用
- nodejs使用xampp中的mysql教程
- Nodejs之HTTP、WEBSOCKET及使用MYSQL
- nodejs mysql
- nodejs mysql
- Nodejs+MySQL
- nodejs+mysql
- nodejs使用
- nodejs使用
- 使用Nodejs实现实时推送MySQL数据库最新信息到客户端
- nodejs连接mysql之使用连接池pool
- 使用 NodeJS+Express+MySQL 实现简单的增删改查
- 【NodeJS】使用forever管理nodejs
- 让老版本IE支持HTML5
- LeetCode---Binary Tree Inorder Traversal
- git基本操作
- ecshop的修改版 ectouch 后台管理账号 暴露的漏洞 发送短信
- npm命令
- nodejs使用MySQL
- [高效算法 deque运用]UVa120 - Stacks of Flapjacks
- [LeetCode 205] Isomorphic Strings
- vector容器v1、v2之间相互赋值的三种方法及易错点详解
- IOS开发之实现App消息推送
- [Hash思想]UVa1152 - 4 Values whose Sum is 0
- POJ - 4047 Garden(线段树成段更新,查询最值)
- iOS实现倒计时功能
- [问题分解]UVa11054 - Wine trading in Gergovia