1)將查詢的結果返還給客戶端
2)訪問數據庫的正常退出
3)返回值的問題
1)將查詢的結果返還給客戶端:
一般的查詢都需要將結果展示出來,否則查詢將顯得沒有意義。在node.js操作postgresql數據庫中,如何將查詢的數據返回回來呢?
(1) select.js
function select(client,selectSQLString,callback)
{
client.query(selectSQLString, function selectCb(error, results)
{
console.log("in select callback function\n");
if (error)
{
console.log('GetData Error: ' + error.message),
client.end();
return;
}
//在執行完查詢以後,結果集被存放在results中,你可以使用console.log(results)打印出來看看
if(results.rowCount > 0)
{
callback(results);
}
});
}
exports.select = select;
(2) client.js
var select = require('./select');
var pg = require('pg');
var conString = "tcp://postgres:postgres@localhost/my";
var client = new pg.Client(conString);
selectSQLString = 'select * from teacher';
client.connect(function(error, results) {
if(error){
console.log('ClientConnectionReady Error: ' + error.message);
client.end();
return;
}
console.log('connection success...\n');
select.select(client,selectSQLString,function(result){
console.log(result);
});
});
//採用回調函數的形式來獲取select.js文件中的查詢結果
執行結果爲:
connection success...
in select callback function
{ command: 'SELECT',
rowCount: 4,
oid: NaN,
rows:
[ { id: '1', name: 'aaa', pwd: '111' },
{ id: '2', name: 'bbb', pwd: '222' },
{ id: '3', name: 'ccc', pwd: '333' },
{ id: '4', name: 'ddd', pwd: '444' } ] }
2) 訪問數據庫的正常退出:
由於node.js的特性,若直接在調用select函數之後就關閉連接,結果可能就和我們預想的不一樣了:
select.js不變,client.js 如下:
var select = require('./select');
var pg = require('pg');
var conString = "tcp://postgres:postgres@localhost/my";
var client = new pg.Client(conString);
selectSQLString = 'select * from teacher';
client.connect(function(error, results) {
if(error){
console.log('ClientConnectionReady Error: ' + error.message);
client.end();
return;
}
console.log('connection success...\n');
select.select(client,selectSQLString,function(result){
console.log(result);
});
client.end();
console.log('Connection closed.\n');
});
運行結果:
connection success...
Connection closed.
可以看出,並沒有將查詢的結果返回回來,連接就關閉了。這是因爲node.js執行到查詢的時候,採用非阻塞的方式,直接跳過執行後面的語句,當查詢執行完畢和調用相應的回調函數。
正確的處理方式爲:client.js:
var select = require('./select');
var pg = require('pg');
var conString = "tcp://postgres:postgres@localhost/my";
var client = new pg.Client(conString);
selectSQLString = 'select * from teacher';
client.connect(function(error, results) {
if(error){
console.log('ClientConnectionReady Error: ' + error.message);
client.end();
return;
}
console.log('connection success...\n');
select.select(client,selectSQLString,function(result){
console.log(result);
client.end();
console.log('Connection closed.\n');
});
});
運行結果:
connection success...
in select callback function
{ command: 'SELECT',
rowCount: 4,
oid: NaN,
rows:
[ { id: '1', name: 'aaa', pwd: '111' },
{ id: '2', name: 'bbb', pwd: '222' },
{ id: '3', name: 'ccc', pwd: '333' },
{ id: '4', name: 'ddd', pwd: '444' } ] }
Connection closed.
3) 返回值的問題:大多數情況下,函數都有返回值
select.js
function select(client,selectSQLString,callback)
{
var content = 'select beginning\n';
client.query(selectSQLString, function selectCb(error, results)
{
console.log("in select callback function");
if (error)
{
console.log('GetData Error: ' + error.message),
client.end();
return;
}
if(results.rowCount > 0)
{
callback(results);
}
});
content += 'select end!\n';
return content;
}
exports.select = select;
client.js
var select = require('./select');
var pg = require('pg');
var conString = "tcp://postgres:postgres@localhost/my";
var client = new pg.Client(conString);
selectSQLString = 'select * from teacher';
client.connect(function(error, results) {
if(error){
console.log('ClientConnectionReady Error: ' + error.message);
client.end();
return;
}
console.log('connection success...\n');
var content = select.select(client,selectSQLString,function(result){
console.log(result);
client.end();
console.log('Connection closed.\n');
});
console.log(content);
});
運行結果:
connection success...
select beginning
select end!
in select callback function
{ command: 'SELECT',
rowCount: 4,
oid: NaN,
rows:
[ { id: '1', name: 'aaa', pwd: '111' },
{ id: '2', name: 'bbb', pwd: '222' },
{ id: '3', name: 'ccc', pwd: '333' },
{ id: '4', name: 'ddd', pwd: '444' } ] }
Connection closed.