国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

node.js中 mysql 增刪改查操作及async,await處理實(shí)例分析

2024-05-06 15:44:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了node.js中 mysql 增刪改查操作及async,await處理。分享給大家供大家參考,具體如下:

要對(duì)mysql進(jìn)行操作,我們需要安裝一個(gè)mysql的庫(kù)。

一、安裝mysql庫(kù)

npm install mysql --save

二、對(duì)mysql進(jìn)行簡(jiǎn)單查詢操作

const mysql = require('mysql');//創(chuàng)建數(shù)據(jù)庫(kù)連接let conn = mysql.createConnection({  //主機(jī)地址  host: '127.0.0.1',  //用戶名  user: 'root',  //密碼  password: '123456',  //數(shù)據(jù)庫(kù)  database: 'test',  //端口  port: 3306,  //字符集  charset: 'utf8'});//連接數(shù)據(jù)庫(kù)conn.connect(function (err) {  if (err) {    throw err;  }  console.log('連接成功');});//查詢數(shù)據(jù)庫(kù)conn.query('select * from tb_user', function (err, data, field) {  if (err) {    throw err;  }  //data表示結(jié)果集數(shù)據(jù),是一個(gè)數(shù)組  console.log(data);  data.forEach(function (value) {    console.log(value.id, value.user_name, value.addr);  });  //表字段的詳細(xì)信息  console.log(field);});//關(guān)閉數(shù)據(jù)庫(kù)連接conn.end();

二、對(duì)mysql進(jìn)行增刪改操作

const mysql = require('mysql');//創(chuàng)建數(shù)據(jù)庫(kù)連接let conn = mysql.createConnection({  //主機(jī)地址  host: '127.0.0.1',  //用戶名  user: 'root',  //密碼  password: '123456',  //數(shù)據(jù)庫(kù)  database: 'test',  //端口  port: 3306,  //字符集  charset: 'utf8'});//連接數(shù)據(jù)庫(kù)conn.connect(function (err) {  if (err) {    throw err;  }  console.log('連接成功');});//插入數(shù)據(jù),query()方法可以對(duì)sql語(yǔ)句進(jìn)行參數(shù)綁定,用?號(hào)作為占位符。conn.query('insert into tb_user values(null, ?, ?)', ['xxx', 'xxx'], function (err, data) {  if (err) {    throw err;  }  if (data && data.affectedRows) {    console.log('插入數(shù)據(jù)成功,id為', data.insertId);  }});//修改數(shù)據(jù)conn.query('update tb_user set user_name = ? where id = ?', ['ggg', 7], function (err, data) {  if (err) {    throw err;  }  if (data && data.affectedRows) {    console.log('修改數(shù)據(jù)成功');  }});//刪除數(shù)據(jù)conn.query('delete from tb_user where id = ?', [5], function (err, data) {  if (err) {    throw err;  }  if (data && data.affectedRows) {    console.log('刪除數(shù)據(jù)成功');  }});//關(guān)閉數(shù)據(jù)庫(kù)連接conn.end();

三、使用mysql連接池來(lái)優(yōu)化對(duì)數(shù)據(jù)庫(kù)的操作

頻繁的連接和斷開(kāi)mysql是比較消耗資源的,我們可以創(chuàng)建一個(gè)連接池,復(fù)用連接池中的連接,提高效率。

const mysql = require('mysql');//創(chuàng)建數(shù)據(jù)庫(kù)連接池let pool = mysql.createPool({  //連接數(shù)量,默認(rèn)是10  connectionLimit: 20,  //主機(jī)地址  host: '127.0.0.1',  //用戶名  user: 'root',  //密碼  password: '123456',  //數(shù)據(jù)庫(kù)  database: 'test',  //端口  port: 3306,  //字符集  charset: 'utf8'});//pool.query()方法可以自動(dòng)的幫我們?cè)谶B接池中獲取可用連接pool.query('select * from tb_user', function (err, data) {  if (err) {    throw err;  }  data.forEach(function (value) {    console.log(value.id, value.user_name, value.addr);  });});//當(dāng)然我們也可以手動(dòng)獲取可用連接pool.getConnection(function (err, conn) {  if (err) {    throw err;  }  conn.query('select * from `order`', function (err, data) {    if (err) {      throw err;    }    data.forEach(function (value) {      console.log(value.id, value.order_id, value.user_id);    });    //連接用完之后,需要釋放,重新放回連接池中。    //注意這里并沒(méi)有銷毀該連接,該連接仍然可用,但需要重新獲取    conn.release();  });});//從連接池中獲取連接時(shí),將觸發(fā)該事件pool.on('acquire', function (conn) {  console.log('獲取連接', conn.threadId);});//在連接池中建立新連接時(shí),將觸發(fā)該事件pool.on('connection', function (conn) {  console.log('建立新連接', conn.threadId);});//等待可用連接時(shí),將觸發(fā)該事件pool.on('enqueue', function () {  console.log('等待可用連接');});//當(dāng)連接釋放回池中時(shí),觸發(fā)該事件pool.on('release', function (conn) {  console.log('連接被釋放回池中', conn.threadId);});//結(jié)束池中所有的連接,不然node.js的事件循環(huán)會(huì)一直保持setTimeout(function () {  pool.end(function (err) {    console.log('關(guān)閉連接池');    console.log(err);  });}, 3000);            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 江华| 甘南县| 成都市| 石门县| 梁河县| 张家港市| 肇源县| 嵊州市| 芷江| 增城市| 汕头市| 洪洞县| 邯郸县| 乐都县| 勐海县| 密云县| 西安市| 海城市| 宁国市| 广灵县| 闵行区| 东方市| 尚志市| 长葛市| 黑水县| 通辽市| 石河子市| 富宁县| 冕宁县| 灵武市| 都匀市| 永丰县| 饶河县| 新化县| 黄浦区| 静乐县| 民县| 嘉定区| 论坛| 城步| 宁陵县|