Node.js訪問PostgresQL資料庫

來源:互聯網
上載者:User

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";

      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"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.