日期:2014-05-16  浏览次数:20643 次

nodejs linux下配置oracle支持
在oracle网站下载oracle数据库客户端连接包

instantclient-basic-linux,instantclient-sdk-linux

解压oracle客户端连接模块

$ unzip instantclient-basic-linux-11.2.0.3.0.zip
$ unzip instantclient-sdk-linux-11.2.0.3.0.zip 
$ sudo mv instantclient_11_2/ /opt/instantclient

$ cd /opt/instantclient
$ sudo ln -s libocci.so.11.1 libocci.so
$ sudo ln -s libclntsh.so.11.1 libclntsh.so

配置环境变量

$ export OCI_INCLUDE_DIR=/opt/instantclient/sdk/include/
$ export OCI_LIB_DIR=/opt/instantclient

进入nodejs目录  安装oracle模块支持

$ cd /usr/local/lib

$ npm install oracle

export LD_LIBRARY_PATH=/opt/instantclient

编写oracle.js文件 测试连接于执行sql是否正常

var oracle = require("oracle");

oracle.connect({ "hostname": "localhost", "user": "demo", "password": "demo", "database": "orcl", "port": 1521}, function(err, connection) {
  if(err) {
   console.log(err);
  }
  // selecting rows 注意 connection.execute 方法必须要三个参数 不然会出错
  connection.execute("SELECT * FROM TEST WHERE ID = :1", ['1'], function(err1, results) {
    // results will be an array of objects
    console.log("query start");
    if(err1) {
        console.log(err1);
    }
   // console.log(results.length);
   for(var i = 0; i < results.length; i++) {
     console.log(results[i].ID);
    }
    connection.close();
  });
});


?

?

终端运行命令 node oracle.js

?