博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs 之 mysql 封装(3)
阅读量:2145 次
发布时间:2019-04-30

本文共 1213 字,大约阅读时间需要 4 分钟。

把对mysql的封装,使用ES7。

代码目录结构如图 -->
在这里插入图片描述

1.DB.js

```javascriptclass DB {
constructor() {
this.mysql = require("mysql") this.dbConfig = require("./db.config.js") } query(sql, params) {
return new Promise((resolve, reject) => {
const connection = this.mysql.createConnection( this.dbConfig ) connection.connect(err => {
if (err) {
console.log("数据库连接失败!") reject(err) } console.log("数据库连接成功!"); }) connection.query(sql, params, (err, results, fileds) => {
if (err) {
console.log("数据库连接失败!") reject(err) } resolve({
results, fileds }) }) connection.end(err => {
if (err) {
console.log("数据库关闭失败!") reject(err) } console.log("数据库关闭成功!") }) }) }}module.exports = new DB()

2.db.coinfig.js

module.exports = {
host: "localhost", user: "root", password: "123456", database: "ceshi"}

转载地址:http://hiegf.baihongyu.com/

你可能感兴趣的文章
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
剑指offer 25.二叉树中和为某一值的路径
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>