cnpm i mysql --save
本地先安装好mysql,这里就不再赘述mysql数据库的安装过程了。在mysql数据库中创建名为 stusystem
的数据库,创建 stu
表,在表中随意添加两条数据。
创建 mysql.js 文件:
const mysql = require('mysql')
// 创建mysql连接
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
connection.connect();
const sql = "select * from stu"; // 执行查询语句
connection.query(sql,(err,result)=>{
if(err) {
console.log(err);
return
}
console.log(result)
connection.destroy(); // 释放资源
})
执行 mysql.js 文件:
node mysql
在控制台打印的结果:
[
RowDataPacket { id: 1, name: '张三', age: 20, sex: '男' },
RowDataPacket { id: 2, name: '李四', age: 21, sex: '男' }
]
(1)新增数据
在 mysql.config.js 文件中编写添加数据的代码:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
connection.connect(); // 建立连接
const sql = "insert into stu(stuname,age,sex) values(?,?,?)" //SQL语句
const sqlParams = ["小明",22,"男"] // 动态参数
connection.query(sql,sqlParams,(err,result)=>{
if(err) {
console.log(err);
return
}
console.log(result); // 打印添加结果
})
connection.end() // 关闭连接
运行 mysql.config.js 文件
node mysql.config
(2)修改数据
代码:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
connection.connect();
const sql = "update stu set stuname=?,age=?,sex=? where sid=?"
const sqlParams = ["韩梅梅",18,"女",3]
connection.query(sql,sqlParams,(err,result)=>{
if(err) {
console.log(err);
return
}
console.log(result);
})
connection.end()
(3)删除数据
代码:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
connection.connect();
const sql = "delete from stu where sid=?"
const sqlParams = [3]
connection.query(sql,sqlParams,(err,result)=>{
if(err) {
console.log(err);
return
}
console.log(result);
})
connection.end()
(4)查询所有数据
代码:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
connection.connect();
const sql = "select * from stu";
connection.query(sql,(err,result)=>{
if(err) {
console.log(err);
return
}
console.log(result);
})
connection.end()
(5)根据条件查询数据
代码:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
connection.connect();
const sql = "select * from stu where stuname=?";
const sqlParams = ["张三"]
connection.query(sql,sqlParams,(err,result)=>{
if(err) {
console.log(err);
return
}
console.log(result);
})
connection.end()
(6)多表查询
先创建2张表,分别添加如下数据:
代码:
const mysql = require('mysql')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
connection.connect();
const sql = "select * from stu,grade where stu.gid = grade.gid";
connection.query(sql,(err,result)=>{
if(err) {
console.log(err);
return
}
console.log(result);
})
connection.end()
在 routes 目录创建路由文件 stu.js,代码如下:
let express = require('express')
let router = express.Router()
let mysql = require('mysql')
let conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'stusystem'
})
router.get("/query",(req,res,next)=>{
conn.connect();
let sql = "select * from stu,grade where stu.gid=grade.gid";
conn.query(sql,(err,result)=>{
if(err){
res.json({
code: 500,
msg: "SQL执行错误",
err
})
}else{
res.json({
code: 200,
msg: "查询成功",
data: result
})
}
conn.destroy();
})
})
module.exports = router
在 app.js 入口文件中配置路由信息,代码如下:
let stuRouter = require('./routes/stu');
app.use('/stu',stuRouter);
执行 npm start
启动服务,使用 postman 测试get请求 http://127.0.0.1:3000/stu/query
。