Node.js访问PostgresQL数据库

php中文网
发布: 2016-06-07 17:26:08
原创
1405人浏览过

Node.js访问PostgresQL数据库,安装PostgresQL模块:进入到/usr/local/lib:发现有一个node_schedules目录,里面放的是你安装的模

1)安装postgresql模块:
  进入到/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";

怪兽AI知识库
怪兽AI知识库

企业知识库大模型 + 智能的AI问答机器人

怪兽AI知识库 51
查看详情 怪兽AI知识库

      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          {
          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的详细信息,或者下载地址请点这里

linux

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号