安装mysql包:npm install mysqljs/mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//加载数据库驱动
const mysql = require('mysql')
//创建数据库连接
const connection = mysql.createConnection({
host : 'localhost',//数据库所在的服务器的域名或IP地址
user : 'root',//登录数据库的账号
password : 'root',//登录数据库的密码
database : 'book'
});
// 执行连接操作
connection.connect();
// 操作数据库
connection.query('select count(*) as total from book', function (error, results, fields) {
if (error) throw error;
console.log('表book中共有', results[0].total + '条数据');
});
// 关闭数据库
connection.end();
向数据库插入数据
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
//加载数据库驱动
const mysql = require('mysql')
//创建数据库连接
const connection = mysql.createConnection({
host : 'localhost',//数据库所在的服务器的域名或IP地址
user : 'root',//登录数据库的账号
password : 'root',//登录数据库的密码
database : 'book'
});
// 执行连接操作
connection.connect();
let sql = 'insert into book set ?'//插入语句
let data = {//插入数据
name: '明朝那些事',
author: '当年明月',
category: '历史',
description: '明朝的故事'
}
// 操作数据库
connection.query(sql, data, function (error, results, fields) {
if (error) throw error;
if(results.affectedRows == 1){//affectedRows:影响的行数
console.log('插入数据成功')
}
});
// 关闭数据库
connection.end();
向数据库更新数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//加载数据库驱动
const mysql = require('mysql')
//创建数据库连接
const connection = mysql.createConnection({
host : 'localhost',//数据库所在的服务器的域名或IP地址
user : 'root',//登录数据库的账号
password : 'root',//登录数据库的密码
database : 'book'
});
// 执行连接操作
connection.connect();
let sql = 'update book set name=?, author=?, category=?, description=? where id=?'//更新语句
let data = ['浪潮之巅', '吴军', '计算机', 'It巨头的兴衰史', 7]//更新数据
// 操作数据库
connection.query(sql, data, function (error, results, fields) {
if (error) throw error;
if(results.affectedRows == 1){
console.log('更新数据成功')
}
});
// 关闭数据库
connection.end();
删除数据库数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//加载数据库驱动
const mysql = require('mysql')
//创建数据库连接
const connection = mysql.createConnection({
host : 'localhost',//数据库所在的服务器的域名或IP地址
user : 'root',//登录数据库的账号
password : 'root',//登录数据库的密码
database : 'book'
});
// 执行连接操作
connection.connect();
let sql = 'delete from book where id=?'//删除语句
let data = [9,10]//删除的id
// 操作数据库
connection.query(sql, data, function (error, results, fields) {
if (error) throw error;
if(results.affectedRows == 1){
console.log('删除数据成功')
}
});
// 关闭数据库
connection.end();
查询数据库数据
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
//加载数据库驱动
const mysql = require('mysql')
//创建数据库连接
const connection = mysql.createConnection({
host : 'localhost',//数据库所在的服务器的域名或IP地址
user : 'root',//登录数据库的账号
password : 'root',//登录数据库的密码
database : 'book'
});
// 执行连接操作
connection.connect();
//无参数查询book表所有数据
/*let sql = 'select * from book'
let data = null*/
//-------------------------------------------
//根据id查询book表数据
let sql = 'select * from book where id = ?'
let data = [5]
// 操作数据库
connection.query(sql, data, function (error, results, fields) {
if (error) throw error;
console.log(results)
});
// 关闭数据库
connection.end();
封装操作数据库的通用api
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const mysql = require('mysql')
exports.base = (sql, data, callback) => {
//创建数据库连接
const connection = mysql.createConnection({
host : 'localhost',//数据库所在的服务器的域名或IP地址
user : 'root',//登录数据库的账号
password : 'root',//登录数据库的密码
database : 'book'
});
// 执行连接操作
connection.connect();
// 操作数据库(数据库操作也是异步)
connection.query(sql, data, function (error, results, fields) {
if (error) throw error;
callback(results)
});
// 关闭数据库
connection.end();
}
测试数据库的通用api
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
const db = require('./db.js')
//插入操作
let sql = 'insert into book set ?'
let data = {
name: '神雕侠侣',
author: '金庸',
category: '爱情',
description: '杨过与小龙女'
}
db.base(sql, data, (result) => {
console.log(result)
})
//更新操作
let sql = 'update book set name=?, author=?, category=?, description=? where id=?'
let data = ['神雕侠侣1', '金庸2', '爱情3', '杨过与小龙女4', 11]
db.base(sql, data, (result) => {
console.log(result)
})
//删除操作
let sql = 'delete from book where id=?'
let data = [11]
db.base(sql, data, (result) => {
console.log(result)
})
//查询操作
let sql = 'select * from book where id=?'
let data = [5]
db.base(sql, data, (result) => {
console.log(result)
})
项目中操作 MySQL
1. 安装
npm install mysql
- 配置
mysql
模块1 2 3 4 5 6 7 8 9
//导入mysql const mysql = require('mysql'); //建立与MySQL数据库的连接 const db = mysql.createPool({ host: '',//数据库ip地址 user: '',//数据库账号 password: '',//数据库密码 database: '',//指定操作哪个数据库 })
2. 查询数据
1
2
3
4
5
6
7
8
//SQL语句
const sqlStr = 'SELECT * FROM users';
db.query(sqlStr, (err, res) => {
//查询失败,err不为null
if(err) return;
//查询成功
...
})
3. 插入数据
1
2
3
4
5
6
7
8
9
10
11
12
13
//待插入的数据对象
const userItem = { username: 'Lily', password: '123456', status: 0 };
//SQL语句,? 用作占位符
const sqlStr = 'INSERT INTO users (username, password, status) VALUES (?, ?, ?)';
//使用数组的形式,依次为 ? 占位符指定对应的值
db.query(sqlStr, [userItem.username, userItem.password, userItem.status], (err, res) => {
//插入失败,err不为null
if(err) return;
//插入成功
if(res.affectedRows === 1){
...
}
})
便捷方法
1
2
3
4
5
6
7
8
9
10
11
12
13
//待插入的数据对象
const userItem = { username: 'Lily', password: '123456', status: 0 };
//SQL语句
const sqlStr = 'INSERT INTO users SET ?';
//直接将数据对象作为占位符的值
db.query(sqlStr, userItem, (err, res) => {
//插入失败,err不为null
if(err) return;
//插入成功
if(res.affectedRows === 1){
...
}
})
4. 更新数据
1
2
3
4
5
6
7
8
9
10
11
12
13
//待更新的数据对象
const userItem = { id: 1, username: 'Jhon', password: '111111' };
//SQL语句,? 用作占位符
const sqlStr = 'UPDATE users SET username=?, password=? WHERE id=?';
//使用数组的形式,依次为 ? 占位符指定对应的值
db.query(sqlStr, [userItem.username, userItem.password, userItem.id], (err, res) => {
//更新失败,err不为null
if(err) return;
//更新成功
if(res.affectedRows === 1){
...
}
})
便捷方法
1
2
3
4
5
6
7
8
9
10
11
12
13
//待更新的数据对象
const userItem = { id: 1, username: 'Jhon', password: '111111' };
//SQL语句
const sqlStr = 'UPDATE users SET ? WHERE id=?';
//直接将数据对象作为占位符的值
db.query(sqlStr, [userItem, userItem.id], (err, res) => {
//更新失败,err不为null
if(err) return;
//更新成功
if(res.affectedRows === 1){
...
}
})
5. 删除数据 推荐根据 id 这样的唯一标识,来删除对应的数据
1
2
3
4
5
6
7
8
9
10
//SQL语句,? 用作占位符
const sqlStr = 'DELETE FROM users WHERE id=?';
db.query(sqlStr, 1, (err, res) => {
//删除失败,err不为null
if(err) return;
//删除成功
if(res.affectedRows === 1){
...
}
})
6. 标记删除 使用 DELETE 语句,会把真正的把数据从表中删除掉。为了保险起见,推荐使用标记删除的形式,来模拟删除的动作。 例如:当用户执行了删除的动作时,我们并没有执行 DELETE 语句把数据删除掉,而是执行了 UPDATE 语句,将这条数据对应 的 status 字段标记为删除即可。
1
2
3
4
5
6
7
const sqlStr = 'UPDATE users SET status=? WHERE id=?;
db.query(sqlStr, [1, 1], (err, res) => {
if(err) return;
if(res.affectedRows === 1){
...
}
})