Node服務(wù)端實(shí)戰(zhàn)之操作數(shù)據(jù)庫示例詳解
連接數(shù)據(jù)庫
本系列是使用node作為服務(wù)器開發(fā)的操作過程記錄,記錄一下主要的內(nèi)容并且整理過程的脈絡(luò),以初學(xué)者的方式將學(xué)習(xí)內(nèi)容記錄下來,從0到1逐步的學(xué)習(xí)node,教程使用過程中用到的是基于express的node框架。
const mysql = require('mysql')
const db = mysql.createPool({
host: 'localhost',
user: 'root',
password: '123123123',
database: 'test',
insecureAuth : true
})
const sql = `select * from new_table`
db.query(sql, (err, results) => {
// console.log(err)
if(err){
console.log(err.message)
}else{
console.log(results) //查詢語句返回的是數(shù)組
}
})
第一次連接數(shù)據(jù)庫馬上就報(bào)錯(cuò)了,還能怎么辦呢,直接谷歌搜吧
ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

大概意思是涉及到一些操作權(quán)限的問題,需要我們到數(shù)據(jù)庫中執(zhí)行這個(gè)語句,如果沒報(bào)錯(cuò)的話大家可以跳過這個(gè)步驟。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '這個(gè)地方替換成你的數(shù)據(jù)庫密碼';
在mysqlworkbrench中執(zhí)行一下即可,然后回到我們的代碼中繼續(xù)執(zhí)行連接數(shù)據(jù)庫的操作

當(dāng)輸出這個(gè)語句的時(shí)候證明已經(jīng)是連接成功的了

insert語句
const obj = {
name:'xiaoma',
password:'666666'
}
const insertSql = `insert into new_table (name,password) values (?,?)`
db.query(insertSql,[obj.name,obj.password],(err,res)=>{
if(err){
console.log(err.message)
}else{
console.log(res)
}
})

affectedRows為影響行,影響行數(shù)為1說明執(zhí)行insert語句成功,所以我們這邊可以修改一下insert成功的判斷
if(res.affectedRows == 1){
console.log('insert success')
}
簡化新增sql
const obj = {
name:'xiaoma',
password:'123123'
}
const insertSql = `insert into new_table SET ?`
db.query(insertSql,obj,(err,res)=>{
if(err){
console.log(err.message)
}
if(res.affectedRows == 1){
console.log('insert success')
}
})
update語句
const updateSql = `Update new_table set name=? ,password=? where id=?`
// const insertSql = `insert into new_table SET ?`
db.query(updateSql,[obj.name,obj.password,obj.id],(err,res)=>{
if(err){
console.log(err.message)
}
if(res.affectedRows == 1){
console.log('insert success')
}
})
//簡化寫法
const updateSql = `Update new_table set ? where id=?`
db.query(updateSql,[obj,obj.id],(err,res)=>{
})
delete語句
const updateSql = `delete from new_table where id=?`
db.query(updateSql,5,(err,res)=>{
if(err){
console.log(err.message)
}
if(res.affectedRows == 1){
console.log('insert success')
}
})以上就是Node服務(wù)端實(shí)戰(zhàn)之操作數(shù)據(jù)庫示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Node服務(wù)端操作數(shù)據(jù)庫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Mongoose實(shí)現(xiàn)虛擬字段查詢的方法詳解
這篇文章主要給大家介紹了關(guān)于Mongoose實(shí)現(xiàn)虛擬字段查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
總結(jié)Node.js中9種fs模塊文件操作方法(文件夾遞歸刪除知識(shí))
這篇文章主要介紹了總結(jié)Node.js中9種fs模塊文件操作方法(文件夾遞歸刪除知識(shí)),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
node如何實(shí)現(xiàn)cmd彈窗交互之inquirer
這篇文章主要介紹了node如何實(shí)現(xiàn)cmd彈窗交互之inquirer問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
NodeJS學(xué)習(xí)筆記之Connect中間件模塊(二)
本文續(xù)上文的內(nèi)容,介紹下nodejs中connect中間件的使用方式及用途,希望大家喜歡。2015-01-01
node.js實(shí)現(xiàn)身份認(rèn)證的示例代碼
本文主要介紹了 node.js實(shí)現(xiàn)身份認(rèn)證的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
node.js 用socket實(shí)現(xiàn)聊天的示例代碼
本篇文章主要介紹了node.js 用socket實(shí)現(xiàn)聊天的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

