Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nodejs uses connection pooling to connect to the database

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Create OptPool.js

Var mysql = require ('mysql')

Function OptPool () {

This.flag = true

This.pool = mysql.createPool ({

Host:'localhost'

User:'root'

Password:''

Database:'mtc'

Port:'3306'

});

This.getPool = function () {

If (this.flag) {

This.pool.on ('connection',function (connection) {

Connection.query ('SET SESSION auto_increment_increment=1')

This.flag = false

});

}

Return this.pool

}

}

Module.exports = OptPool

Create mysqlPool.js

Var OptPool = require ('. / models/OptPool')

Var optPool = new OptPool ()

Var pool = optPool.getPool ()

Pool.getConnection (function (err,conn) {

/ / Delete

Var deleteSql = "DELETE FROM mtc_users WHERE user_id =?"

Var deleteParams = ['3']

Conn.query (deleteSql,deleteParams,function (err,rs) {

If (err) {

Console.log (err)

Return

}

Console.log ('delete success')

Conn.release ()

})

/ modify

/ / var updateSql = "UPDATE mtc_users SET user_pass =? WHERE user_id =?"

/ / var updateParams = ['7891012]

/ / conn.query (updateSql,updateParams,function (err,rs) {

/ / if (err) {

/ / console.log (err)

/ / return

/ /}

/ / console.log ('update success')

/ / conn.release ()

/ /})

/ query

/ / var selectSql = "SELECT * FROM mtc_users"

/ / var selectParams = []

/ / conn.query (selectSql,selectParams,function (err,rs) {

/ / if (err) {

/ / console.log (err)

/ / return

/ /}

/ / for (var I = 0bot I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report