nodeJS 操作 MySQL
1. 在数据库(如例所示lzq)里建1张表(如例所示teachers)。
2. 安装nodeJS版MySQL驱动:
npm install mysql;
3. 执行startSQL.js文件:
node startSQL.js
4. 测试环境:
node.js 0.6.12
5. 共2个js文件:startSQL.js and CRUD.js.
startSQL.js ------------------------------->
var CRUD = require('./CRUD'),
sys = require('util'),
client = require('mysql').createClient({'host':'localhost',
'port':3306,
'user':'root',
'password':'123456'}),
ClientConnectionReady = function(client){
// - just for test, these variables can be from request process program. ---------------------------------
var value = ['10', 'fillp', 'abc'],
insertSQLString = 'INSERT INTO teachers SET id = ?, name = ? , pwd = ?',
//teachers is the table name.
selectSQLString = 'SELECT * FROM teachers',
updateSQLString = "update teachers set NAME='ipone' where ID=4",
deleteSQLString = 'delete from teachers where ID=10';
// -----------------------------------
console.log('ClientConnectionReady'),
console.log('\n');
client.query('USE lzq', function(error, results) { // lzq is the name of database.
if(error){
console.log('ClientConnectionReady Error: ' + error.message),
client.end();
return;
}
console.log('Connecting to MySQL...'),
console.log('Connected to MySQL automatically'),
console.log('\n'),
//CRUD._insert(client,insertSQLString,value),
CRUD._select(client,selectSQLString),
//CRUD._update(client,updateSQLString),
//CRUD._delete(client,deleteSQLString),
console.log('connection success...'),
console.log('\n'),
client.end(),
console.log('Connection closed'),
console.log('\n');
});
};
ClientConnectionReady(client);
CRUD.js -------------------------------->
var CRUD = {
_insert : function(client,insertSQLString,value){
client.query(insertSQLString, value,
function(error, results) {
if(error) {
console.log("ClientReady Error: " + error.message),
client.end();
return;
}
console.log('Inserted: ' + results.affectedRows + ' row.'),
console.log('insert success...');
}
);
},
_select : function(client,selectSQLString){
client.query(selectSQLString,
function selectCb(error, results, fields) {
if (error) {
console.log('GetData Error: ' + error.message),
client.end();
return;
}
if(results.length > 0){
var firstResult,
resultSet = '';
for(var i = 0, len = results.length; i < len; i++){
firstResult = results[i],
resultSet += 'id: ' + firstResult['id'] + ' ' + 'name: ' + firstResult['name'] + ' ' + 'pwd: ' + firstResult['pwd'] + '\n';
}
}
console.log(resultSet);
/* 添加功能:使查询结果集返回到客户端并保证此函数的通用性. */
}
);
},
_update : function(client,updateSQLString){
client.query(updateSQLString,
function(error, results) {
if(error) {
console.log("ClientReady Error: " + error.message),
client.end();
return;
}
console.log('update success...');
}
);
},
_delete : function(client,deleteSQLString){
client.query(deleteSQLString,
function(error, results) {
if(error) {
console.log("ClientReady Error: " + error.message),
client.end();
return;
}
console.log('delete success...');
}
);
}
};
exports._insert = CRUD._insert,
exports._select = CRUD._select,
exports._update = CRUD._update,
exports._delete = CRUD._delete;