node.js访问postgresql 数据库
来源:互联网 发布:北京餐饮软件排名 编辑:程序博客网 时间:2024/05/04 01:53
1)安装pg模块:
进入到/usr/local/lib:发现有一个node_schedules目录,里面放的是你安装的模块
npm install -g node-gyp
export $PATH=$PATH:/usr/local/pgsql/bin/
npm install pg //-g 表示全局
2)连接数据库并访问
连接字符串=“tcp:// 用户名 : 密码 @localhost:5432/ 库名”;
1) 事件形式:
var pg = require('pg');
var MATH = require('math');
var constring = "tcp://postgres:1234@localhost/my";
var client = new pg.Client(constring);
client.connect();
client.query("create temp table beatle(name varchar(10),height integer)");
client.query("insert into beatle(name,height) values('john',50)");
client.query("insert into beatle(name,height) values($1,$2)",['brown',68]);
var query = client.query("select * from beatle");
query.on('row',function(row){
console.log(row);
console.log("Beatle name:%s",row.name);
console.log("beatle height:%d' %d\"",MATH.floor(row.height/12),row.height%12);
});
query.on('end',function(){
client.end();
});
测试成功,输出内容为:
{ name: 'john', height: 50 }
Beatle name:john
beatle height:4' 2"
{ name: 'brown', height: 68 }
Beatle name:brown
beatle height:5' 8"
2)回调函数形式:
var pg = require('pg');
var conString = "tcp://postgres:1234@localhost/my";
var client = new pg.Client(conString);
client.connect(function(err) {
client.query('SELECT NOW() AS "theTime"', function(err, result) {
console.log(result.rows[0].theTime);
client.end();
})
});
测试成功,输出结果为:
Wed Jan 23 2013 14:30:12 GMT+0800 (CST)
3) 用回调方式实现上面的事件形式:
var pg = require('pg');
var MATH = require('math');
var constring = "tcp://postgres:1234@localhost/my";
var client = new pg.Client(constring);
client.connect(function(err){
client.query("create temp table beatle(name varchar(10),height integer)");
client.query("insert into beatle(name,height) values('john',50)");
client.query("insert into beatle(name,height) values($1,$2)",['brown',68]);
client.query("select * from beatle ",function(err,result){
console.log(result);
for(var i=0;i<result.rowCount;i++ )
{
console.log(result.rows[i]);
console.log("Beatle name:%s",result.rows[0].name);
console.log("beatle height:%d' %d\"",MATH.floor(result.rows[0].height/12),result.rows[0].height%12);
}
client.end();
});
});
测试成功,输出结果为:
{ command: 'SELECT',
rowCount: 2,
oid: NaN,
rows: [ { name: 'john', height: 50 }, { name: 'brown', height: 68 } ] }
{ name: 'john', height: 50 }
Beatle name:john
beatle height:4' 2"
{ name: 'brown', height: 68 }
Beatle name:john
beatle height:4' 2"
- node.js访问postgresql 数据库
- node.js 操作 postgresql 数据库
- Node.js 连接 postgreSQL数据库
- node.js 操作postgresql数据库(2)
- JS访问数据库[非Node.js]
- node.js 访问redis数据库,pub/sub
- Node.js中连接PostgreSql数据库的方法
- Node.js 连接 postgreSQL数据库 (使用连接池pool)
- C#访问postgresql数据库
- C#访问PostGreSQL数据库
- PostgreSQL 数据库访问配置
- 远程访问postgresql数据库
- C#访问postgresql数据库
- C# 访问PostgreSQL 数据库
- .NET 访问PostgreSQL数据库
- .NET 访问PostgreSQL数据库
- Node.js开发手册(四)-Redis数据库访问
- Node.js开发手册(四)-Redis数据库访问
- JD2912汽车继电器(双触点性能参数
- HTTP 学习之请求方法
- 让多个Viewer对应一个属性视图
- HDU 1465(错排公式)
- display:none与visible:hidden的区别
- node.js访问postgresql 数据库
- Android OpenGL ES 基础:绘制三角形
- Java动态代理
- Fusioncharts XML问题
- .net 链接及操作 Oracle
- 将uiui从minigui1.6移植到minigui3.0
- 补码
- MVC3中的tempdata,viewdata,viewbag
- ie6、ie7dom 元素重新渲染