Node.js對(duì)MySQL數(shù)據(jù)庫(kù)的增刪改查實(shí)戰(zhàn)記錄
在項(xiàng)目中操作數(shù)據(jù)庫(kù)的三大步驟
- 安裝操作 MySQL 數(shù)據(jù)庫(kù)的第三方模塊(mysql)
- 通過(guò) mysql 模塊連接到 MySQL 數(shù)據(jù)庫(kù)
- 通過(guò) mysql 模塊執(zhí)行 SQL 語(yǔ)句
操作數(shù)據(jù)庫(kù)的具體步驟
一:安裝MySQL模塊及express模塊
MySQL模塊是托管于npm上的第三方模塊,我們可以運(yùn)行下方命令安裝MySQL第三方包,通過(guò)它來(lái)建立node.js項(xiàng)目與MySQL數(shù)據(jù)庫(kù)的連接,進(jìn)而操作數(shù)據(jù)庫(kù)(以下代碼在終端中運(yùn)行)
//安裝mysql第三方模塊 npm i mysql //安裝express第三方模塊 npm i express
二:通過(guò)express創(chuàng)建一個(gè)服務(wù)器
// 引入express const express = require('express'); // 創(chuàng)建服務(wù)器 const app = express(); // 啟動(dòng)服務(wù)器 app.listen(80, () => { console.log('http://127.0.0.1'); })
三:配置MySQL模塊
在使用 MySQL 模塊操作 MySQL 數(shù)據(jù)庫(kù)之前,必須先對(duì) mysql模塊進(jìn)行必要的配置,主要的配置步驟如下:
// 1.引入mysql const mysql = require('mysql'); // 2.建立與mysql數(shù)據(jù)庫(kù)連接 var db = mysql.createPool({ host: '127.0.0.1', // 數(shù)據(jù)庫(kù)的 ip 地址 user: 'root', // 登錄數(shù)據(jù)庫(kù)的賬號(hào) password: '123456', // 登錄數(shù)據(jù)庫(kù)的密碼 database: 'web67' // 指定要操作哪個(gè)數(shù)據(jù)庫(kù) });
四:測(cè)試 mysql 模塊能否正常工作
調(diào)用 db.query() 函數(shù),指定要執(zhí)行的 SQL 語(yǔ)句,通過(guò)回調(diào)函數(shù)拿到執(zhí)行的結(jié)果
// 測(cè)試mysql模塊是否能正常運(yùn)行,查找所有數(shù)據(jù)并顯示至頁(yè)面 db.query('select * from one', (err, data) => { if (err) return console.log(err.message); if (data.length === 0) return console.log('數(shù)據(jù)庫(kù)無(wú)數(shù)據(jù)'); console.log(data) //data是從數(shù)據(jù)庫(kù)中查找到的數(shù)據(jù) }) });
以上代碼全部準(zhǔn)備完畢后開(kāi)始對(duì)MySQL數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行增刪改查:
SELECT:查詢(xún)one數(shù)據(jù)表中所有的數(shù)據(jù):
案例代碼:
// 獲取數(shù)據(jù)庫(kù)中的數(shù)據(jù) app.get('/selectuser', (req, res) => { // 查看數(shù)據(jù)庫(kù)連接是否成功 db.query('select * from one', (err, data) => { //err不為空則表示錯(cuò)誤 if (err) return console.log(err.message); if (data.length === 0) return console.log('獲取失敗'); res.send(data) }) });
INSERT INTO:向數(shù)據(jù)庫(kù)中添加數(shù)據(jù):
案例代碼:
這里用到了post請(qǐng)求,需要通過(guò)req.body接收客戶(hù)端請(qǐng)求的數(shù)據(jù),并通過(guò)app.use(express.urlencoded({extended:false}))代碼行將數(shù)據(jù)進(jìn)行解析(見(jiàn)下方完整代碼)
// 向數(shù)據(jù)庫(kù)添加數(shù)據(jù) app.post('/insertuser', (req, res) => { // 接收客戶(hù)端請(qǐng)求的數(shù)據(jù) const body = req.body; // 構(gòu)建sql語(yǔ)句 const sql = 'insert into one set ?' // 將客戶(hù)請(qǐng)求的數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中 db.query(sql, body, (err, data) => { if (err) return console.log(err.message); if (data.affectedRows !== 1) return console.log('添加失敗'); res.send({ status: 0, msg: '添加數(shù)據(jù)成功' }) }) })
UPADTE:修改數(shù)據(jù)庫(kù)中的數(shù)據(jù):
案例代碼:
// 修改數(shù)據(jù)庫(kù)中數(shù)據(jù) app.post('/updateuser', (req, res) => { //接收客戶(hù)端請(qǐng)求的數(shù)據(jù) const body = req.body; //構(gòu)建修改的sql語(yǔ)句 const sql = 'update one set uname=? where id=?'; db.query(sql, [body.uname, body.id], (err, data) => { if (err) return console.log(err.message); if (data.affectedRows !== 1) return console.log('修改失敗'); res.send({ status: 0, msg: '修改數(shù)據(jù)成功' }) }) })
DELETE:刪除數(shù)據(jù)庫(kù)中的數(shù)據(jù):
案例代碼:
// 刪除數(shù)據(jù)庫(kù)中的數(shù)據(jù) app.get('/deleteuser/:id', (req, res) => { // 獲取客戶(hù)端提交的數(shù)據(jù),req.params通過(guò):冒號(hào)匹配動(dòng)態(tài)參數(shù) const id = req.params.id; // 構(gòu)建刪除的sql語(yǔ)句,一般為了防止數(shù)據(jù)被永久性刪除,我們會(huì)通過(guò)update將該條數(shù)據(jù)對(duì)應(yīng)的 狀態(tài)碼先修改為0 const sql = 'update one set status=0 where id=?'; // 執(zhí)行sql db.query(sql, id, (err, data) => { if (err) return console.log(err.message); if (data.affectedRows !== 1) return console.log('刪除失敗'); res.send({ status: 0, msg: '刪除成功' }) }) })
到這里通過(guò)express中間件對(duì)MySQL數(shù)據(jù)庫(kù)的增刪改查就完成啦,【完整代碼】如下:
// 通過(guò)express中間件實(shí)現(xiàn)對(duì)mysql數(shù)據(jù)庫(kù)的增刪改查 const express = require('express'); // 創(chuàng)建一個(gè)服務(wù)器 const app = express(); // 解析客戶(hù)端請(qǐng)求的數(shù)據(jù) app.use(express.urlencoded({ extended: false })); // 引入mysql操作數(shù)據(jù)庫(kù) const mysql = require('mysql'); // 配置數(shù)據(jù)庫(kù) const db = mysql.createPool({ host: '127.0.0.1', //數(shù)據(jù)庫(kù)ip地址 user: 'root', //數(shù)據(jù)庫(kù)賬號(hào) password: '123456', //數(shù)據(jù)庫(kù)密碼 database: 'web67' //數(shù)據(jù)庫(kù)名稱(chēng) }); // 獲取數(shù)據(jù)庫(kù)中的數(shù)據(jù) app.get('/selectuser', (req, res) => { // 查看數(shù)據(jù)庫(kù)連接是否成功 db.query('select * from one', (err, data) => { if (err) return console.log(err.message); if (data.length === 0) return console.log('獲取失敗'); res.send(data) }) }); // 向數(shù)據(jù)庫(kù)添加數(shù)據(jù) app.post('/insertuser', (req, res) => { // 接收客戶(hù)端請(qǐng)求的數(shù)據(jù) const body = req.body; // 構(gòu)建sql語(yǔ)句 const sql = 'insert into one set ?' // 將客戶(hù)請(qǐng)求的數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中 db.query(sql, body, (err, data) => { if (err) return console.log(err.message); if (data.affectedRows !== 1) return console.log('添加失敗'); res.send({ status: 0, msg: '添加數(shù)據(jù)成功' }) }) }) // 修改數(shù)據(jù)庫(kù)中數(shù)據(jù) app.post('/updateuser', (req, res) => { const body = req.body; const sql = 'update one set uname=? where id=?'; db.query(sql, [body.uname, body.id], (err, data) => { if (err) return console.log(err.message); if (data.affectedRows !== 1) return console.log('修改失敗'); res.send({ status: 0, msg: '修改數(shù)據(jù)成功' }) }) }) // 刪除數(shù)據(jù)庫(kù)中的數(shù)據(jù)(指定id) app.get('/deleteuser/:id', (req, res) => { const id = req.params.id; //id為動(dòng)態(tài)參數(shù),所以要通過(guò)req.params獲取 const sql = 'update one set status=0 where id=?' db.query(sql, id, (err, data) => { if (err) return console.log(err.message); if (data.affectedRows !== 1) return console.log('刪除數(shù)據(jù)失敗'); res.send({ status: 0, msg: '刪除成功' }) }) }) // 啟動(dòng)服務(wù)器 app.listen(80, () => { console.log('http://127.0.0.1'); })
總結(jié)
到此這篇關(guān)于Node.js對(duì)MySQL數(shù)據(jù)庫(kù)增刪改查的文章就介紹到這了,更多相關(guān)Node.js對(duì)MySQL增刪改查內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。