1.安裝mysql組件
npm install mysql
或者在package.json中添加
"dependencies": {
"mysql": ""
}
而后鍵入
npm install
2疯搅、創(chuàng)建連接測(cè)試腳本databaseConnect.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '服務(wù)器地址',
user : '管理員賬戶',
password : '管理員密碼'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
3搁胆、測(cè)試腳本
node databaseConnect.js
4懦胞、向數(shù)據(jù)庫(kù)添加數(shù)據(jù)
var usr = {
memberName: req.body.memberName,
memberSchoolNumber: req.body.memberSchoolNumber,
memberClassName: req.body.memberClassName,
memberTel: req.body.memberTel,
memberQQ: req.body.memberQQ,
memberSex: req.body.memberSex,
memberTeam: req.body.memberTeam,
memberMessage: req.body.memberMessage,
};
connection.query('insert into tblMemberRegisInfo set ?', usr, function (err, result) {
if (err) throw err;
console.log('inserted complete');
console.log(result);
console.log('\n');
});
5箩艺、查詢數(shù)據(jù)庫(kù)數(shù)據(jù)
//顯示查詢數(shù)據(jù)
connection.query('select * from tblMemberRegisInfo', function (err, rows, fields) {
if (err) throw err;
console.log('selected after deleted');
for (var i = 0, usr; usr = rows[i++];) {
console.log('user name=' + usr.memberName);
}
console.log('\n');
});