当前位置: 首页 > 工具软件 > PG.js > 使用案例 >

node.js 操作postgresql数据库(2)

昝卓
2023-12-01
在上一篇 node.js操作postgresql数据库中,还有很多需要进行完善的地方,而这篇文章主要完善以下几点:
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.




 类似资料: