-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
51 lines (45 loc) · 1.15 KB
/
db.js
File metadata and controls
51 lines (45 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Created by WIGER on 2017/6/3.
*/
var mysql=require('mysql');
var option={
host: 'localhost',
user: 'root',
password: '123',
database: 'addressList'
}
var DB={};
module.exports=DB;
//sql语句执行
DB.exec=function (sql, values, after) {
var connection = mysql.createConnection(option);
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
}
console.log('connected as id ' + connection.threadId);
connection.query(sql || '', values || [], function(err, rows) {
after(err, rows);
});
//关闭数据库连接
connection.end();
});
connection.on('error', function(err) {
if (err.errno != 'ECONNRESET') {
after("err01", false);
throw err;
} else {
after("err02", false);
}
});
};
//事务连接
DB.getConnection=function(callback){
var connection=mysql.createConnection(option);
connection.connect(function(err){
if(err){
console.error('error connecting: ' + err.stack);
}
callback(err,connection);
});
}