add utils
This commit is contained in:
31
utils/dbUtils.js
Normal file
31
utils/dbUtils.js
Normal file
@@ -0,0 +1,31 @@
|
||||
//引入MySQL
|
||||
const mysql = require('mysql');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let globalConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json'), 'utf8'));
|
||||
let config = {
|
||||
...globalConfig.mysql,
|
||||
};
|
||||
console.log(config);
|
||||
|
||||
async function query(sql) {
|
||||
//通过MySQL中方法创建链接对象
|
||||
var connection = mysql.createConnection(config);
|
||||
//开始连接
|
||||
connection.connect();
|
||||
//执行SQL语句 (添加、删除、更新、查询)
|
||||
connection.query(sql, (err, result) => {
|
||||
if (err) {
|
||||
console.error('err', err);
|
||||
return;
|
||||
}
|
||||
console.log('result', result);
|
||||
})
|
||||
//最后需要关闭连接
|
||||
connection.end();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
query: query,
|
||||
}
|
Reference in New Issue
Block a user