Node.js對(duì)MySQL數(shù)據(jù)庫的增刪改查實(shí)戰(zhàn)記錄
在項(xiàng)目中操作數(shù)據(jù)庫的三大步驟
- 安裝操作 MySQL 數(shù)據(jù)庫的第三方模塊(mysql)
- 通過 mysql 模塊連接到 MySQL 數(shù)據(jù)庫
- 通過 mysql 模塊執(zhí)行 SQL 語句
操作數(shù)據(jù)庫的具體步驟
一:安裝MySQL模塊及express模塊
MySQL模塊是托管于npm上的第三方模塊,我們可以運(yùn)行下方命令安裝MySQL第三方包,通過它來建立node.js項(xiàng)目與MySQL數(shù)據(jù)庫的連接,進(jìn)而操作數(shù)據(jù)庫(以下代碼在終端中運(yùn)行)
//安裝mysql第三方模塊 npm i mysql //安裝express第三方模塊 npm i express
二:通過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ù)庫之前,必須先對(duì) mysql模塊進(jìn)行必要的配置,主要的配置步驟如下:
// 1.引入mysql const mysql = require('mysql'); // 2.建立與mysql數(shù)據(jù)庫連接 var db = mysql.createPool({ host: '127.0.0.1', // 數(shù)據(jù)庫的 ip 地址 user: 'root', // 登錄數(shù)據(jù)庫的賬號(hào) password: '123456', // 登錄數(shù)據(jù)庫的密碼 database: 'web67' // 指定要操作哪個(gè)數(shù)據(jù)庫 });
四:測(cè)試 mysql 模塊能否正常工作
調(diào)用 db.query() 函數(shù),指定要執(zhí)行的 SQL 語句,通過回調(diào)函數(shù)拿到執(zhí)行的結(jié)果
// 測(cè)試mysql模塊是否能正常運(yùn)行,查找所有數(shù)據(jù)并顯示至頁面 db.query('select * from one', (err, data) => { if (err) return console.log(err.message); if (data.length === 0) return console.log('數(shù)據(jù)庫無數(shù)據(jù)'); console.log(data) //data是從數(shù)據(jù)庫中查找到的數(shù)據(jù) }) });
以上代碼全部準(zhǔn)備完畢后開始對(duì)MySQL數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行增刪改查:
SELECT:查詢one數(shù)據(jù)表中所有的數(shù)據(jù):
案例代碼:
// 獲取數(shù)據(jù)庫中的數(shù)據(jù) app.get('/selectuser', (req, res) => { // 查看數(shù)據(jù)庫連接是否成功 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ù)庫中添加數(shù)據(jù):
案例代碼:
這里用到了post請(qǐng)求,需要通過req.body接收客戶端請(qǐng)求的數(shù)據(jù),并通過app.use(express.urlencoded({extended:false}))代碼行將數(shù)據(jù)進(jìn)行解析(見下方完整代碼)
// 向數(shù)據(jù)庫添加數(shù)據(jù) app.post('/insertuser', (req, res) => { // 接收客戶端請(qǐng)求的數(shù)據(jù) const body = req.body; // 構(gòu)建sql語句 const sql = 'insert into one set ?' // 將客戶請(qǐng)求的數(shù)據(jù)插入到數(shù)據(jù)庫中 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ù)庫中的數(shù)據(jù):
案例代碼:
// 修改數(shù)據(jù)庫中數(shù)據(jù) app.post('/updateuser', (req, res) => { //接收客戶端請(qǐng)求的數(shù)據(jù) const body = req.body; //構(gòu)建修改的sql語句 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ù)庫中的數(shù)據(jù):
案例代碼:
// 刪除數(shù)據(jù)庫中的數(shù)據(jù) app.get('/deleteuser/:id', (req, res) => { // 獲取客戶端提交的數(shù)據(jù),req.params通過:冒號(hào)匹配動(dòng)態(tài)參數(shù) const id = req.params.id; // 構(gòu)建刪除的sql語句,一般為了防止數(shù)據(jù)被永久性刪除,我們會(huì)通過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: '刪除成功' }) }) })
到這里通過express中間件對(duì)MySQL數(shù)據(jù)庫的增刪改查就完成啦,【完整代碼】如下:
// 通過express中間件實(shí)現(xiàn)對(duì)mysql數(shù)據(jù)庫的增刪改查 const express = require('express'); // 創(chuàng)建一個(gè)服務(wù)器 const app = express(); // 解析客戶端請(qǐng)求的數(shù)據(jù) app.use(express.urlencoded({ extended: false })); // 引入mysql操作數(shù)據(jù)庫 const mysql = require('mysql'); // 配置數(shù)據(jù)庫 const db = mysql.createPool({ host: '127.0.0.1', //數(shù)據(jù)庫ip地址 user: 'root', //數(shù)據(jù)庫賬號(hào) password: '123456', //數(shù)據(jù)庫密碼 database: 'web67' //數(shù)據(jù)庫名稱 }); // 獲取數(shù)據(jù)庫中的數(shù)據(jù) app.get('/selectuser', (req, res) => { // 查看數(shù)據(jù)庫連接是否成功 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ù)庫添加數(shù)據(jù) app.post('/insertuser', (req, res) => { // 接收客戶端請(qǐng)求的數(shù)據(jù) const body = req.body; // 構(gòu)建sql語句 const sql = 'insert into one set ?' // 將客戶請(qǐng)求的數(shù)據(jù)插入到數(shù)據(jù)庫中 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ù)庫中數(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ù)庫中的數(shù)據(jù)(指定id) app.get('/deleteuser/:id', (req, res) => { const id = req.params.id; //id為動(dòng)態(tài)參數(shù),所以要通過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ù)庫增刪改查的文章就介紹到這了,更多相關(guān)Node.js對(duì)MySQL增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- node.js 開發(fā)指南 – Node.js 連接 MySQL 并進(jìn)行數(shù)據(jù)庫操作
- Node.js數(shù)據(jù)庫操作之查詢MySQL數(shù)據(jù)庫(二)
- Node.js下向MySQL數(shù)據(jù)庫插入批量數(shù)據(jù)的方法
- Node.js操作mysql數(shù)據(jù)庫增刪改查
- Node.js數(shù)據(jù)庫操作之連接MySQL數(shù)據(jù)庫(一)
- node.js平臺(tái)下的mysql數(shù)據(jù)庫配置及連接
- 從零學(xué)習(xí)node.js之mysql數(shù)據(jù)庫的操作(五)
- Linux下為Node.js程序配置MySQL或Oracle數(shù)據(jù)庫的方法
- Node.js實(shí)現(xiàn)連接mysql數(shù)據(jù)庫功能示例
- node.js如何操作MySQL數(shù)據(jù)庫
- Node.js實(shí)現(xiàn)http請(qǐng)求服務(wù)與Mysql數(shù)據(jù)庫操作方法詳解
- node.js對(duì)于數(shù)據(jù)庫MySQL基本操作實(shí)例總結(jié)【增刪改查】
相關(guān)文章
詳解MySQL中事務(wù)隔離級(jí)別的實(shí)現(xiàn)原理
這篇文章主要介紹了MySQL中事務(wù)隔離級(jí)別的實(shí)現(xiàn)原理,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下2021-01-01MYSQL ON UPDATE CURRENT_TIMESTAMP當(dāng)字段值發(fā)生改變時(shí)才會(huì)更
本文主要介紹了MYSQL ON UPDATE CURRENT_TIMESTAMP當(dāng)字段值發(fā)生改變時(shí)才會(huì)更新記錄的時(shí)間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01mysql 數(shù)據(jù)庫安裝經(jīng)驗(yàn)問題匯總
這篇文章主要介紹了mysql 數(shù)據(jù)庫安裝經(jīng)驗(yàn)問題匯總,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09mysql 操作總結(jié) INSERT和REPLACE
用于操作數(shù)據(jù)庫的SQL一般分為兩種,一種是查詢語句,也就是我們所說的SELECT語句,另外一種就是更新語句,也叫做數(shù)據(jù)操作語句。2009-07-07