接下來(lái),我們以mySQL數(shù)據(jù)庫(kù)為例檀何,介紹如何在Node.js中使用數(shù)據(jù)庫(kù)蝇裤。
首先,需要先使用npm包管理工具安裝MySQL客戶端開發(fā)包
npm install mysql
引入數(shù)據(jù)庫(kù)
var mysql = require('mysql')
建立連接
案例如下
var mysql = require('mysql')
var connection = mysql.createConnection({
host:'172.16.20.103',
port:3308,
database:'test',
user:'JRJ_Win',
password:'FT%^$fjYR56'
})
connection.connect(function(err){
if(err){
console.log(err)
console.log('與mysql數(shù)據(jù)庫(kù)連接失敗')
}else{
console.log('與mysql數(shù)據(jù)庫(kù)連接成功')
connection.end(function(err){
if (err) {
console.log('關(guān)閉mysql數(shù)據(jù)庫(kù)失敗')
}else{
console.log('關(guān)閉mysql數(shù)據(jù)庫(kù)成功')
}
})
}
})
案例效果
執(zhí)行數(shù)據(jù)庫(kù)的基本處理
connection.query(sql,[parameters],callback)
案例如下
var mysql = require('mysql')
var connection = mysql.createConnection({
host:'172.16.20.103',
port:3308,
database:'test',
user:'JRJ_Win',
password:'FT%^$fjYR56'
})
connection.connect(function(err){
if(err){
console.log(err)
console.log('與mysql數(shù)據(jù)庫(kù)連接失敗')
}else{
console.log('與mysql數(shù)據(jù)庫(kù)連接成功')
connection.query('INSERT INTO win.luck_fine SET?',{username:'范小飯',age:18,sex:'女',dec:'愛(ài)吃'},function(err){
if(err){
console.log(err)
console.log('插入數(shù)據(jù)失敗')
}else{
console.log('插入數(shù)據(jù)成功')
connection.query('SELECT * FROM ??',['win.luck_fine'],function(err,result){
if(err){
console.log('查詢數(shù)據(jù)失敗')
}else{
console.log(result);
connection.end();
}
})
}
})
}
})
案例效果
以數(shù)據(jù)流的方式處理查詢數(shù)據(jù)
有時(shí)频鉴,用戶也許會(huì)查詢大量數(shù)據(jù)并希望單獨(dú)處理每一條單查詢的數(shù)據(jù)栓辜,在mysql模塊中,connection對(duì)象的query方法返回一個(gè)可用于處理數(shù)據(jù)流數(shù)據(jù)的對(duì)象砚殿。如果使用queru方法的對(duì)象啃憎,那么在query方法中不能使用callback參數(shù)指定回調(diào)函數(shù)
var mysql = require('mysql')
var fs = require('fs')
var connection = mysql.createConnection({
host:'172.16.20.103',
port:3308,
database:'test',
multipleStatements:true,
user:'JRJ_Win',
password:'FT%^$fjYR56'
})
var out = fs.createWriteStream('./message.txt')
out.on('error',function(err){
console.log('寫文件操作失敗');
process.exit();
})
connection.connect(function(err){
if(err){
console.log('與mysql數(shù)據(jù)庫(kù)連接失敗')
}else{
console.log('與mysql數(shù)據(jù)庫(kù)連接成功')
var query = connection.query('select * from win.luck_fine')
query.on('error',function(err){
console.log('寫文件操作失敗');
process.exit();
})
query.on('fields',function(fields){
var str = '';
fields.forEach(function(field){
if(str != ''){
str += String.fromCharCode(9);
}else{
str += fields.username;
}
})
out.write(str+'\r\n');
})
query.on('result',function(row){
connection.pause()
out.write(row.username+row.dec+'\r\n',function(err){
connection.resume()
})
})
query.on('end',function(row){
console.log('數(shù)據(jù)全部寫入完畢')
connection.end();
})
}
})
案例效果
創(chuàng)建連接池
在開發(fā)一個(gè)web應(yīng)用程序時(shí)辛萍,建立一個(gè)數(shù)據(jù)庫(kù)鏈接所消耗的性能成本是比較高的羡藐,在服務(wù)器應(yīng)用程序中贩毕,如果為每一個(gè)接收到的客戶端請(qǐng)求都建立一個(gè)或多個(gè)數(shù)據(jù)庫(kù)連接,將嚴(yán)重降低應(yīng)用程序的性能仆嗦,因此辉阶,在服務(wù)器應(yīng)用程序中,同城需要為多個(gè)成數(shù)據(jù)連接創(chuàng)建并維護(hù)一個(gè)連接池,當(dāng)連接不再需要使用時(shí)谆甜,這些連接可以緩存在連接池中垃僚,當(dāng)接受到下一個(gè)客戶端請(qǐng)求時(shí),可以從連接池中取出連接并重新利用规辱,不需要再重新建立數(shù)據(jù)庫(kù)連接谆棺。
var pool = mysql.createPool(options)
案例如下
var mysql = require('mysql')
var pool = mysql.createPool({
host:'172.16.20.103',
port:3308,
database:'test',
user:'JRJ_Win',
password:'FT%^$fjYR56'
})
pool.getConnection(function(err,connection){
if(err){
console.log(err)
console.log('與mysql數(shù)據(jù)庫(kù)連接失敗')
}else{
console.log('與mysql數(shù)據(jù)庫(kù)連接成功')
connection.query('select * from win.luck_fine',function(err,rows){
if(err){
console.log('查詢數(shù)據(jù)操作失敗')
}else{
console.log(rows)
pool.end();
}
})
}
})
案例效果
同志們,點(diǎn)贊不花錢呀! 程序猿不易罕袋,且行且珍惜~