01 |
var mysql = require( 'mysql' ); |
02 |
//填寫數據庫鏈接信息,可查詢數據庫詳情頁 |
03 |
var username = 'username' ; |
04 |
var password = 'passwork' ; |
05 |
var db_host = '127.0.0.1' ; |
06 |
var db_port = 3306; |
07 |
var db_name = 'test' ; |
08 |
var option = { |
09 |
host: db_host, |
10 |
port: db_port, |
11 |
user: username, |
12 |
password: password, |
13 |
database: db_name |
14 |
}; |
15 |
16 |
function _exec(sqls,values,after) { |
17 |
var client = mysql.createConnection(option); |
18 |
19 |
20 |
client.connect( function (err){ |
21 |
if (err) { |
22 |
|
23 |
console.log(err); |
24 |
return ; |
25 |
} |
26 |
27 |
client.query(sqls || '' , values || [], function (err,r){ |
28 |
after(err,r); |
29 |
}); |
30 |
client.end(); |
31 |
32 |
}); |
33 |
client.on( 'error' , function (err) { |
34 |
if (err.errno != 'ECONNRESET' ) { |
35 |
after( "err01" , false ); |
36 |
throw err; |
37 |
} else { |
38 |
after( "err02" , false ); |
39 |
} |
40 |
}); |
41 |
} |
42 |
exports.exec = _exec; |
node-mysql庫:https://github.com/felixge/node-mysqlnode