mysql
使用mysql库
bash
npm i -s mysql
npm i -s mysql
创建util/dbconfig.js
js
const mysql = require('mysql')
const config = { // mysql设置
host: 'localhost',
port: '3306',
user: 'root',
password: '123456',
database: 'api'
}
const sqlConnect = (sql, sqlArr, callback) => { // mysql连接函数
const pool = mysql.createPool(config) // 创建mysql连接池
pool.getConnection((err, conn) => { // 连接池开始连接
if (err) return console.log(`连接mysql数据库失败,${err}`)
console.log('操作mysql数据库成功')
conn.query(sql, sqlArr, callback) // 三个参数分别为sql语句,sql语句查询参数,回调函数
conn.release() // 释放连接
})
}
module.exports = {
sqlConnect
}
const mysql = require('mysql')
const config = { // mysql设置
host: 'localhost',
port: '3306',
user: 'root',
password: '123456',
database: 'api'
}
const sqlConnect = (sql, sqlArr, callback) => { // mysql连接函数
const pool = mysql.createPool(config) // 创建mysql连接池
pool.getConnection((err, conn) => { // 连接池开始连接
if (err) return console.log(`连接mysql数据库失败,${err}`)
console.log('操作mysql数据库成功')
conn.query(sql, sqlArr, callback) // 三个参数分别为sql语句,sql语句查询参数,回调函数
conn.release() // 释放连接
})
}
module.exports = {
sqlConnect
}
使用连接函数实现增删改查
js
const dbconfig = require("../../util/dbconfig")
// 增
const addData = (req, res) => {
const sql = `insert into test set name=?, phone=?, sex=?`
console.log(req.body);
const sqlArr = [req.body.name, req.body.phone, req.body.sex]
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '新增数据失败',
'err': err
})
console.log('新增数据失败')
} else {
res.send({
'code': 200,
'msg': '新增数据成功'
})
console.log('新增数据成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 删
const deleteData = (req, res) => {
const sql = 'delete from test where id=?'
const sqlArr = [req.query.id]
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '删除数据失败',
'err': err
})
console.log('删除数据失败');
} else {
res.send({
'code': 200,
'msg': '删除数据成功'
})
console.log('删除数据成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 改
const updatedData = (req, res) => {
const sql = 'update test set phone=? where id=?'
const sqlArr = [req.query.phone, req.query.id];
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '更改数据失败',
'err': err
})
console.log('更改数据失败')
} else {
res.send({
'code': 200,
'msg': '更改数据成功'
})
console.log('更改数据成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 查
const selectData = (req, res) => {
console.log(req.query)
const sql = 'select * from test'
const sqlArr = []
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '查询失败',
'err': err
})
console.log('查询失败')
} else {
res.send({
'code': 200,
'msg': '查询数据成功',
data
})
console.log('查询成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
module.exports = {
addData, deleteData, updatedData, selectData
}
const dbconfig = require("../../util/dbconfig")
// 增
const addData = (req, res) => {
const sql = `insert into test set name=?, phone=?, sex=?`
console.log(req.body);
const sqlArr = [req.body.name, req.body.phone, req.body.sex]
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '新增数据失败',
'err': err
})
console.log('新增数据失败')
} else {
res.send({
'code': 200,
'msg': '新增数据成功'
})
console.log('新增数据成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 删
const deleteData = (req, res) => {
const sql = 'delete from test where id=?'
const sqlArr = [req.query.id]
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '删除数据失败',
'err': err
})
console.log('删除数据失败');
} else {
res.send({
'code': 200,
'msg': '删除数据成功'
})
console.log('删除数据成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 改
const updatedData = (req, res) => {
const sql = 'update test set phone=? where id=?'
const sqlArr = [req.query.phone, req.query.id];
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '更改数据失败',
'err': err
})
console.log('更改数据失败')
} else {
res.send({
'code': 200,
'msg': '更改数据成功'
})
console.log('更改数据成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 查
const selectData = (req, res) => {
console.log(req.query)
const sql = 'select * from test'
const sqlArr = []
const callBack = (err, data) => {
if (err) {
res.send({
'code': 404,
'msg': '查询失败',
'err': err
})
console.log('查询失败')
} else {
res.send({
'code': 200,
'msg': '查询数据成功',
data
})
console.log('查询成功')
}
}
dbconfig.sqlConnect(sql, sqlArr, callBack)
}
module.exports = {
addData, deleteData, updatedData, selectData
}