首頁 web前端 js教程 實戰學習:聊聊Node.js怎麼操作資料庫

實戰學習:聊聊Node.js怎麼操作資料庫

Dec 15, 2022 pm 09:14 PM
nodejs node

這篇文章分享Node.js服務端實戰,介紹一下Node操作資料庫的方法,希望對大家有幫助!

實戰學習:聊聊Node.js怎麼操作資料庫

本系列是使用node作為伺服器開發的操作過程記錄,記錄一下主要​​的內容並且整理過程的脈絡,以初學者的方式將學習內容記錄下來,從0到1逐步的學習node,教程使用過程中用到的是基於express的node框架。 【相關教學推薦:nodejs影片教學程式設計教學

連線資料庫##
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) //查询语句返回的是数组
  }
})
登入後複製

第一次連接資料庫馬上就報錯了,還能怎麼辦呢,直接谷歌搜吧

ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
登入後複製

實戰學習:聊聊Node.js怎麼操作資料庫

#大概意思是涉及到一些操作權限的問題,需要我們到資料庫中執行這個語句,如果沒報錯的話大家可以跳過這個步驟。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '这个地方替换成你的数据库密码';
登入後複製

在mysqlworkbrench中執行一下即可,然後回到我們的程式碼繼續執行連接資料庫的動作

實戰學習:聊聊Node.js怎麼操作資料庫

當輸出這個語句的時候證明已經是連線成功的了

實戰學習:聊聊Node.js怎麼操作資料庫

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)
    }
})
登入後複製

實戰學習:聊聊Node.js怎麼操作資料庫

affectedRows為影響行,影響行數為1說明執行i​​nsert語句成功,所以我們這邊可以修改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相關知識,請造訪:

nodejs 教學

以上是實戰學習:聊聊Node.js怎麼操作資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

nodejs和tomcat區別 nodejs和tomcat區別 Apr 21, 2024 am 04:16 AM

nodejs和tomcat區別

nodejs和vuejs區別 nodejs和vuejs區別 Apr 21, 2024 am 04:17 AM

nodejs和vuejs區別

nodejs是後端框架嗎 nodejs是後端框架嗎 Apr 21, 2024 am 05:09 AM

nodejs是後端框架嗎

nodejs安裝目錄裡的npm與npm.cmd檔有什麼差別 nodejs安裝目錄裡的npm與npm.cmd檔有什麼差別 Apr 21, 2024 am 05:18 AM

nodejs安裝目錄裡的npm與npm.cmd檔有什麼差別

nodejs是後端開發語言嗎 nodejs是後端開發語言嗎 Apr 21, 2024 am 05:09 AM

nodejs是後端開發語言嗎

nodejs中的全域變數有哪些 nodejs中的全域變數有哪些 Apr 21, 2024 am 04:54 AM

nodejs中的全域變數有哪些

nodejs和java的差別大嗎 nodejs和java的差別大嗎 Apr 21, 2024 am 06:12 AM

nodejs和java的差別大嗎

nodejs和java選哪個 nodejs和java選哪個 Apr 21, 2024 am 04:40 AM

nodejs和java選哪個

See all articles