亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    實(shí)戰(zhàn)學(xué)習(xí):聊聊Node.js怎么操作數(shù)據(jù)庫(kù)

    本篇文章分享Node.js服務(wù)端實(shí)戰(zhàn),介紹一下Node操作數(shù)據(jù)庫(kù)的方法,希望對(duì)大家有所幫助!

    實(shí)戰(zhàn)學(xué)習(xí):聊聊Node.js怎么操作數(shù)據(jù)庫(kù)

    node.js極速入門(mén)課程:進(jìn)入學(xué)習(xí)

    本系列是使用node作為服務(wù)器開(kāi)發(fā)的操作過(guò)程記錄,記錄一下主要的內(nèi)容并且整理過(guò)程的脈絡(luò),以初學(xué)者的方式將學(xué)習(xí)內(nèi)容記錄下來(lái),從0到1逐步的學(xué)習(xí)node,教程使用過(guò)程中用到的是基于express的node框架。【相關(guān)教程推薦:nodejs視頻教程、編程教學(xué)】

    連接數(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) //查詢語(yǔ)句返回的是數(shù)組   } })
    登錄后復(fù)制

    第一次連接數(shù)據(jù)庫(kù)馬上就報(bào)錯(cuò)了,還能怎么辦呢,直接谷歌搜吧

    ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    登錄后復(fù)制

    實(shí)戰(zhàn)學(xué)習(xí):聊聊Node.js怎么操作數(shù)據(jù)庫(kù)

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

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

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

    實(shí)戰(zhàn)學(xué)習(xí):聊聊Node.js怎么操作數(shù)據(jù)庫(kù)

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

    實(shí)戰(zhàn)學(xué)習(xí):聊聊Node.js怎么操作數(shù)據(jù)庫(kù)

    insert語(yǔ)句

    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)     } })
    登錄后復(fù)制

    實(shí)戰(zhàn)學(xué)習(xí):聊聊Node.js怎么操作數(shù)據(jù)庫(kù)

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

     if(res.affectedRows == 1){     console.log('insert success') }
    登錄后復(fù)制

    簡(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')     } })
    登錄后復(fù)制

    update語(yǔ)句

    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)化寫(xiě)法 const updateSql = `Update  new_table set ? where id=?` db.query(updateSql,[obj,obj.id],(err,res)=>{ })
    登錄后復(fù)制

    delete語(yǔ)句

    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')     } })
    登錄后復(fù)制

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)