溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Node.js如何操作數(shù)據(jù)庫(kù)

發(fā)布時(shí)間:2022-12-16 09:35:28 來源:億速云 閱讀:137 作者:iii 欄目:web開發(fā)

今天小編給大家分享一下Node.js如何操作數(shù)據(jù)庫(kù)的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

連接數(shù)據(jù)庫(kù)

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ù)庫(kù)馬上就報(bào)錯(cuò)了,還能怎么辦呢,直接谷歌搜吧

ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

Node.js如何操作數(shù)據(jù)庫(kù)

大概意思是涉及到一些操作權(quán)限的問題,需要我們到數(shù)據(jù)庫(kù)中執(zhí)行這個(gè)語句,如果沒報(bào)錯(cuò)的話大家可以跳過這個(gè)步驟。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '這個(gè)地方替換成你的數(shù)據(jù)庫(kù)密碼';

在mysqlworkbrench中執(zhí)行一下即可,然后回到我們的代碼中繼續(xù)執(zhí)行連接數(shù)據(jù)庫(kù)的操作

Node.js如何操作數(shù)據(jù)庫(kù)

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

Node.js如何操作數(shù)據(jù)庫(kù)

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如何操作數(shù)據(jù)庫(kù)

affectedRows為影響行,影響行數(shù)為1說明執(zhí)行insert語句成功,所以我們這邊可以修改一下insert成功的判斷

 if(res.affectedRows == 1){
    console.log('insert success')
}

簡(jiǎn)化新增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')
    }
})

//簡(jiǎn)化寫法
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.js如何操作數(shù)據(jù)庫(kù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI