node.js 操作postgresql資料庫

來源:互聯網
上載者:User

 1)將查詢的結果返還給用戶端:   一般的查詢都需要將結果展示出來,否則查詢將顯得沒有意義。在node.js操作postgresql資料庫中,如何將查詢的資料返回回來呢?  (1)  select.js [javascript] <span style="font-size:14px;">function select(client,selectSQLString,<span style="color:#FF0000;">callback</span>)  {      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)          {              <span style="color:#FF0000;">callback(results);</span>            }      });  }    exports.select = select;  </span>    (2) client.js[javascript]  <span style="font-size:14px;">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,<span style="color:#FF0000;">function(result){            console.log(result);      }</span>);  });</span>    //採用回呼函數的形式來擷取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 如下: [javascript] <span style="font-size:14px;">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');  });  </span>      運行結果:              connection success...                  Connection closed.     可以看出,並沒有將查詢的結果返回回來,串連就關閉了。這是因為node.js執行到查詢的時候,採用非阻塞的方式,直接跳過執行後面的語句,當查詢執行完畢和調用相應的回呼函數。 正確的處理方式為:client.js: [javascript] <span style="font-size:14px;">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');      });  });  </span>  運行結果: 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 [javascript]  <span style="font-size:14px;">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;  </span>   client.js[javascript]  <span style="font-size:14px;">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);  });  </span>   運行結果:  connection success...   select beginning  select end!   in select callback function  { command: 'SELECT',    rowCount: 4,    oid: NaN,    rows: www.2cto.com     [ { 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.     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.