• node 连接MySQL及其分装, 连接池连接


    const mysql = require('mysql')
    
    const config = require('./../../config/config.default')
    
    var connection = mysql.createConnection({
      host: config.database.HOST,
      user: config.database.USERNAME,
      password: config.database.PASSWORD,
      database: config.database.DATABASE
    })
    
    connection.connect(function(err) {
      if (err) {
        console.error('error connecting: ' + err.stack)
        return
      }
      console.log('Database connection successful, connected as id ' + connection.threadId)
    })
    
    //查询
    // connection.query(`select * from area`, function(err, results, fields) {
    //   if (err) throw err
    //   console.log(results[0])
    //   console.log(fields[0])
    // })
    
    
    
    //关闭连接
    // connection.end(function(err) {
    //   if (err) {
    //     console.error('数据库关闭失败' + err)
    //   }
    //   console.log('数据库关闭')
    // })
    
    module.exports = connection
    // 分装
    
    const mysql = require('mysql')
    
    const config = require('./../../config/config.default')
    
    module.exports = {
      query: function(sql, params, callback) {
        var connection = mysql.createConnection({
          host: config.database.HOST,
          user: config.database.USERNAME,
          password: config.database.PASSWORD,
          database: config.database.DATABASE
        })
        connection.connect(function(err) {
          if (err) {
            console.error('error connecting: ' + err.stack)
            throw err
          }
          console.log('Database connection successful, connected as id ' + connection.threadId)
          connection.query(sql, params, function(err, results, fields) {
            if (err) throw err
            callback && callback(results, fields)
            connection.end(function(err) {
              if (err) {
                console.error('数据库关闭失败' + err)
                throw err 
              }
              console.log('数据库关闭')
            })
          })    
        })
      },
      // 连接池连接
      queryPool: function(sql, params, callback) {
        var pool = mysql.createPool({
          host: config.database.HOST,
          user: config.database.USERNAME,
          password: config.database.PASSWORD,
          database: config.database.DATABASE
        })
        pool.getConnection(function(err, connection) {
          // 使用连接
          connection.query(sql, params, function(err, results, fields) {
            if (err) throw err
            callback && callback(results, fields)
            connection.release()     
            console.info('连接完成,不再使用,返回到连接池')
          })
        })
      }
    }
    
    
    // 调用
    const db = require('./index')
    
    
    db.query(`select * from area`, [], function(result, fields) {
      console.log('查询结果:')
      console.log(result[0].name)
    })
    
    db.queryPool(`select * from area`, [], function(result, fields) {
      console.log('查询结果:')
      console.log(result[0])
    })
    

      

    const db = require('../db')
    const query = db.queryPoolPromise
    
    /**
     * @author afacode
     * SQL 操作
     * @date 2018.08.15
     */
    let findAllUser = (value, col = '*') => {
      let _sql = `select ${col} from user_login where user_id = ?;`
      return query(_sql, value)
    }
    // 测试
    async function user() {
      let user = await findAllUser([22], ['username', 'password'])
      
    console.log(user)
    }
    
    user()
    

      

      

  • 相关阅读:
    cocos2dx的内存管理机制
    cocos2d-x多分辨率适配方案:setDesignResolutionSize使用
    在lua脚本中使用我们自定义的精灵类
    cocos2d-x内存管理(见解)
    MongoDB学习笔记(2):数据库操作及CURD初步
    剑指Offer:树的子结构
    剑指Offer:反转链表
    剑指Offer:调整数组顺序使奇数位于偶数前面【21】
    剑指Offer:链表中倒数第k个结点【22】
    剑指Offer:合并两个排序的链表【25】
  • 原文地址:https://www.cnblogs.com/lfqcode/p/9473861.html
Copyright © 2020-2023  润新知