走入到/usr/local/lib:开采有一个node_schedules目录,里面放的是您安装的模块
npm install -g node_gyp export $PATH=$PATH:/usr/local/pgsql/…

  

}  

  1. 在数据库(如例所示lzq)里建1张表(如例所示teachers)。

  2. 安装nodeJS版MySQL驱动:
       npm install mysql;

  3. 执行startSQL.js文件:
       node startSQL.js

  4. 测量检验蒙受:
       node.js 0.6.12

  5. 共2个js文件:startSQL.js and CRUD.js.

     client.query(“insert into beatle(name,height) values(‘john’,50)”);

    });  

[javascript] 

nodeJS 操作 MySQL

2)连接数据库并拜望

            for(var i = 0, len = results.rowCount; i < len; i++)  

selectSQLString = ‘select * from teacher’;  

操作 MySQL 1.
在数据库(如例所示lzq)里建1张表(如例所示teachers)。 2.
装置nodeJS版MySQL驱动: npm install mysql; 3.
试行startSQL.js文件:…

    oid: NaN,

在你安装了node.js,postgresql数据库(不自然是本机)和相应的模块的底蕴上,在数据库中国建筑工程总公司表teacher。并插入几条测验数据,那儿插入的测量试验数据为:

<span style=”font-size:14px;”>var select = require(‘./select’);  

startSQL.js ——————————->
01
var CRUD = require(‘./CRUD’),
02
    sys = require(‘util’),
03
    client = require(‘mysql’).createClient({‘host’:’localhost’,
04
                                            ‘port’:3306,
05
                                            ‘user’:’root’,
06
                                            ‘password’:’admin’}),
07
    ClientConenctionReady = function(client){
08
        var value = [’10’,’fillp’,’abc’],
09
        insertSQLString = ‘insert into teachers SET id = ?, name = ? ,
pwd = ?’,
10
        selectSQLString = ‘select * from teachers’,
11
        updateSQLString = “update teachers set NAME=’ipone’ where
ID=4”,
12
        deleteSQLString = ‘delete from teachers where ID=10’;
13
        
14
        console.log(‘ClientConnectionReady’),
15
        console.log(‘\n’)
16
        // lzq is the name of database.
17
        client.query(‘USE lzq’, function(error, results) {
18
            if(error){
19
                console.log(‘ClientConnectionReady Error: ‘ +
error.message),
20
                client.end();
21
                return;
22
            }
23
            console.log(‘Connecting to MySQL…’),
24
            console.log(‘Connected to MySQL automatically’),
25
            console.log(‘\n’),
26
            console.log(‘connection success…’),
27
            console.log(‘\n’),
28
 
29
            client.end(),
30
            console.log(‘Connection closed’),
31
            console.log(‘\n’);
32
        });
33
    };
CRUD.js ——————————–>

          client.end();

function _update(client,updateSQLString)  

 

摘自  YouDoce

      client.query(“insert into beatle(name,height)
values($1,$2)”,[‘brown’,68]);

  

     [ { id: ‘1’, name: ‘aaa’, pwd: ‘111’ },

view sourceprint?
01
var CRUD = {
02
     _insert : function(client,insertSQLString,value)
03
     {
04
         client.query(insertSQLString, value,  function(error,
results)
05
             {
06
                     if(error)
07
                     {
08
                        console.log(“ClientReady Error: ” +
error.message),
09
                        client.end();
10
                        return;
11
                     }
12
                        console.log(‘Inserted: ‘ + results.affectedRows

      Beatle name:john

        if (error)  

 

  • ‘ row.’),
    13
                            console.log(‘insert success…’);
    14
                 }
    15
             );
    16
         },
    17
         _select : function(client,selectSQLString)
    18
         {
    19
             client.query(selectSQLString,  function selectCb(error,
    results, fields)
    20
                 {
    21
                             if (error)
    22
                             {
    23
                                console.log(‘GetData Error: ‘ +
    error.message),
    24
                                client.end();
    25
                                return;
    26
                             }
    27
                            if(results.length > 0)
    28
                            {
    29
                                var firstResult,
    30
                                resultSet = ”;
    31
                                for(var i = 0, len = results.length; i <
    len; i++)
    32
                                {
    33
                                    firstResult = results[i],
    34
                                    resultSet += ‘id:’ + firstResult[‘id’]
  • ” + ‘name:’ + firstResult[‘name’] + ” + ‘pwd:’ +
    firstResult[‘pwd’] + ‘\n’; 
    35
                                }
    36
                            }
    37
                            console.log(resultSet);
    38
                            /*
    增添效用:使查询结果集重临到客商端并确定保证此函数的通用性. */
    39
                 }
    40
            );
    41
         },
    42
         _update : function(client,updateSQLString)
    43
         {
    44
            client.query(updateSQLString,function(error, results)
    45
                {
    46
                    if(error)
    47
                    {
    48
                        console.log(“ClientReady Error: ” +
    error.message),
    49
                        client.end();
    50
                        return;
    51
                    }
    52
                    console.log(‘update success…’);
    53
                }
    54
            );
    55
         },
    56
         _delete : function(client,deleteSQLString)
    57
         {
    58
            client.query(deleteSQLString,  function(error, results)
    59
                {
    60
                    if(error)
    61
                    {
    62
                        console.log(“ClientReady Error: ” +
    error.message),
    63
                        client.end();
    64
                        return;
    65
                    }
    66
                    console.log(‘delete success…’);
    67
                }
    68
            );
    69
         }
    70
    };
    71
        exports._insert = CRUD._insert,
    72
        exports._select = CRUD._select,
    73
        exports._update = CRUD._update,
    74
        exports._delete = CRUD._delete;

      var client = new pg.Client(conString);

var pg = require(‘pg’);  

        [ { id: ‘1’, name: ‘aaa’, pwd: ‘111’ },

      { command: ‘SELECT’,

id:4 name:ddd pwd:444

 

      var constring =
“tcp://postgres:[email protected]/my”;

 

   
能够看到,并不曾将查询的结果回到回来,连接就关门了。那是因为node.js试行到查询的时候,接纳非阻塞的章程,直接跳过推行前面包车型地铁言语,当查问实践完成和调用相应的回调函数。

 

            {  

    [ { id: ‘1’, name: ‘aaa’, pwd: ‘111’ },

      var pg = require(‘pg’); 

            console.log(‘GetData Error: ‘ + error.message),  

        if(results.rowCount > 0)  

     client.query(“insert into beatle(name,height)
values($1,$2)”,[‘brown’,68]);

        }  

            client.end();  

     var client = new pg.Client(constring);

in select callback function

 

      beatle height:4′ 2″

{  

   select.js

 

    client.query(updateSQLString,function(error, results)  

client.connect(function(error, results) {  

      console.log(result.rows[0].theTime);

id:2 name:bbb pwd:222

var conString =
“tcp://postgres:[email protected]/my”;
 

          }

  

var conString =
“tcp://postgres:[email protected]/my”;
 

      client.query(“select * from beatle “,function(err,result){

{  

 

    client.end();

}  

 

    rows: [ { name: ‘john’, height: 50 }, { name: ‘brown’, height: 68 }
] }

insert success…

    select.select(client,selectSQLString,function(result){  

          console.log(“Beatle name:%s”,result.rows[0].name);

                resultSet += ‘id:’ + firstResult[‘id’] + ‘ ‘ + ‘name:’

      { id: ‘3’, name: ‘ccc’, pwd: ‘333’ },

      Beatle name:john

 

 

      });

 

  

       

[cpp]  

</span>  

    3) 用回调方式实现地点的事件情势:

        if(error)  

});  

    

        {  

 

      var client = new pg.Client(constring);

    f._select(client,selectSQLString);  

  

     client.connect();

    f._select(client,selectSQLString);  

selectSQLString = ‘select * from teacher’;  

          {

    console.log(“insert beginning”);  

3)  重回值的难题:大比很多动静下,函数都有再次回到值

    console.log(“Beatle name:%s”,row.name);

select beginning

  

     query.on(‘end’,function(){

    console.log(“select end\n”);  

        console.log(‘Connection closed.\n’);  

 

        if(error)  

</span>  

 

var conString =
“tcp://postgres:[email protected]/my”;
 

[javascript] 

      Beatle name:john

insert into teacher values(‘2′,’bbb’,’222′);  

   运行结果:

 

        if(results.rowCount > 0)  

 

          console.log(result);

function _select(client,selectSQLString)  

        client.end();  

    rowCount: 2,

insert into teacher values(‘3′,’ccc’,’333′);  

 

     query.on(‘row’,function(row){

    console.log(“delete beginning”);  

exports.select = select;  

      { name: ‘brown’, height: 68 }

Connecting to postgres…

 connection success…

    console.log(row);

    client.query(selectSQLString,  function selectCb(error, results,
fields)  

var pg = require(‘pg’);  

      { name: ‘john’, height: 50 }

 

 

      Wed Jan 23 2013 14:30:12 GMT+0800 (CST)

delete success…

            <span
style=”color:#FF0000;”>callback(results);</span>    

      client.connect(function(err){

 

        console.log(‘ClientConnectionReady Error: ‘ + error.message);  

     测量试验成功,输出内容为:

  

       if(results.rowCount > 0)  

      beatle height:4′ 2″

        if(error)  

    select.select(client,selectSQLString,<span
style=”color:#FF0000;”>function(result){    

      Beatle name:brown

        {  

[javascript] 

  export $PATH=$PATH:/usr/local/pgsql/bin/

 

        {  

     });

}  

  select.js不变,client.js 如下:

      client.query(“create temp table beatle(name varchar(10),height
integer)”);

  

    if(error){  

  1) 事件方式:

insert into teacher values(‘1′,’aaa’,’111′);  

 

      { name: ‘john’, height: 50 }

select beginning

    content += ‘select end!\n’;  

  npm install pg              //-g 表示全局

 

       { id: ‘4’, name: ‘ddd’, pwd: ‘444’ } ] }

     var MATH = require(‘math’);

    f._insert(client,insertSQLString,value);  

  //选用回调函数的样式来收获select.js文件中的查询结果

   

3) 运转node client.js,结果如下:

            callback(results);  

      { name: ‘brown’, height: 68 }

  

 
 一般的询问都亟需将结果体现出来,不然查询将体现未有意义。在node.js操作postgresql数据库中,怎么样将查询的数目重回回来吗?

  npm install -g node_gyp

        }  

      { id: ‘4’, name: ‘ddd’, pwd: ‘444’ } ] }

    2)回调函数格局:

 

    }  

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注