node.js 操作postgresql数据库(2)
2013-01-28 14:08
471 查看
在上一篇 node.js操作postgresql数据库中,还有很多需要进行完善的地方,而这篇文章主要完善以下几点:
1)将查询的结果返还给客户端
2)访问数据库的正常退出
3)返回值的问题
1)将查询的结果返还给客户端:
一般的查询都需要将结果展示出来,否则查询将显得没有意义。在node.js操作postgresql数据库中,如何将查询的数据返回回来呢?
(1) select.js
(2) client.js
//采用回调函数的形式来获取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 如下:
运行结果:
connection success...
Connection closed.
可以看出,并没有将查询的结果返回回来,连接就关闭了。这是因为node.js执行到查询的时候,采用非阻塞的方式,直接跳过执行后面的语句,当查询执行完毕和调用相应的回调函数。
正确的处理方式为:client.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' } ] }
Connection closed.
3) 返回值的问题:大多数情况下,函数都有返回值
select.js
client.js
运行结果:
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.
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.
相关文章推荐
- node.js 操作 postgresql 数据库
- 使用node.js中的async模块进行数据库的同步操作
- Node.js操作Firebird数据库教程
- Node.js 数据库 简单操作
- MFC+mongodb+nodejs 数据库的读取与写入操作
- Node.js 连接 postgreSQL数据库
- nodejs mongodb好友关系数据库设计与操作
- Node.js 连接 MySQL 并进行数据库操作
- nodejs mongoose 操作mongodb 数据库封装
- Node.js操作Firebird数据库教程
- Node.js的DAO层,关于数据库操作的db.js
- Node.js数据库操作之查询MySQL数据库(二)
- Node.js Express 从入门到菜鸟(三)——node.js+express+mongodb 操作数据库
- node.js操作数据库
- node.js 开发指南 – Node.js 连接 MySQL 并进行数据库操作
- Node.js 连接 postgreSQL数据库 (使用连接池pool)
- Node.js MongoDB的安装 配置 数据库的CRUD操作 索引 学习笔记文档
- Node.js 连接 MySQL 并进行数据库操作
- node.js访问postgresql 数据库
- Node.js连接postgreSQL并进行数据操作