nodejs Oracle 连接配置安装

http://loopback.io/doc/en/lb3/Oracle-installer-command.html

https://github.com/strongloop/loopback-connector-oracle

https://github.com/oracle/node-oracledb/blob/master/INSTALL.md#-7-node-oracledb-installation-on-windows

https://community.oracle.com/docs/DOC-931127

 

先要解决node-gyp编译环境

https://github.com/nodejs/node-gyp#installation

https://github.com/felixrieseberg/windows-build-tools

例子1

http://www.cnblogs.com/alunchen/p/5546821.html

例子2

http://www.jianshu.com/p/83833b86eeca

一 前言

  众所周知,Node可以很轻松的连接PostgreSQL,MySQL,Redis,Mogondb等开源数据库,但是想使用Node连接Oracle一直非常难,不仅仅在Windowns操作系统上,即使是Linux上也非常难编译或运行成功,笔者经过多次失败,最近找到一个oracle官方提供的库oracledb,之所以叫oracledb,是之前npm上已经存在一个山寨的oracle包,安装过程如下:

npm install oracle

  当然并不怎么好编译通过和使用,不过oralce官方也没办法了,只能自己叫oracledb了。下文讲述如何安装和简单使用,以linux为例。

二 安装

2.1 安装前提

  本机要预先安装gcc4.7及以上版本(编译node需要使用)。

2.2 安装nodejs

假设把node的安装包下载在/opt目录。

cd /opt
# 解压
tar -Jxf node-v6.9.4-linux-x64.tar.xz
# 设置环境变量
vi /etc/profile
#编辑文件内如如下:
export NODEHOME=/opt/node-v7.9.0-linux-x64
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$NODEHOME/bin
#保存退出

#重启用profile文件
source /etc/profile
# 检测node安装情况
[root@localhost testoracle]# node -v
v7.9.0
[root@localhost testoracle]# npm -v
4.2.0

如上步骤,安装完毕,是不是很简单?

2.3 安装Oracle Instant Client

  从oralce官网下载 'Basic' and 'SDK',假如下载位置在/opt/oracle。

cd /opt/oracle
unzip instantclient-basic-linux.x64-12.2.0.1.0.zip
unzip instantclient-sdk-linux.x64-12.2.0.1.0.zip
mv instantclient_12_2 instantclient
cd instantclient
ln -s libclntsh.so.12.1 libclntsh.so
# 设置环境变量
vi /etc/profile
#编辑内容如下
export LD_LIBRARY_PATH=/opt/oracle/instantclient:$LD_LIBRARY_PATH
export OCI_LIB_DIR=/opt/oracle/instantclient
export OCI_INC_DIR=/opt/oracle/instantclient/sdk/include
#保存退出

#重启用profile文件
source /etc/profile

2.4 安装oracledb
  之前安装都已经完成了,这一步会非常简单。

[root@localhost opt]# mkdir oracletest
[root@localhost opt]# cd oracletest
[root@localhost oracletest]# npm install oracledb

> oracledb@1.13.1 install /opt/oracletest/node_modules/oracledb
> node-gyp rebuild

gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/7.9.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/opt/aa/node_modules/oracledb/.node-gyp"
make: Entering directory `/opt/aa/node_modules/oracledb/build'
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsOracle.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsPool.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsConnection.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsResultSet.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsMessages.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsIntLob.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiEnv.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiEnvImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiException.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiExceptionImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiConnImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiDateTimeArrayImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiPoolImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiStmtImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiUtils.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiLob.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiCommon.o
  SOLINK_MODULE(target) Release/obj.target/oracledb.node
  COPY Release/oracledb.node
make: Leaving directory `/opt/oracletest/node_modules/oracledb/build'
/opt/oracletest
└─┬ oracledb@1.13.1 
  └── nan@2.5.1 

npm WARN enoent ENOENT: no such file or directory, open '/opt/oracletest/package.json'
npm WARN oracletest No description
npm WARN oracletest No repository field.
npm WARN oracletest No README data
npm WARN oracletest No license field.

稍等片刻,哗哗的安装全部结束了。

三 测试

在/opt/oracletest中新建一个测试的js文件,用于访问oracle数据库。

var oracledb = require('oracledb');
var config = {
  user:'mytest',
  password:'password',
  connectString : "192.168.0.107:1521/orcl"
};
oracledb.getConnection(
  config,
  function(err, connection)
  {
    if (err) {
      console.error(err.message);
      return;
    }
    connection.execute("SELECT * from aa",
      function(err, result)
      {
        if (err) {
          console.error(err.message);
          doRelease(connection);
          return;
        }
        //打印返回的表结构
        console.log(result.metaData);
        //打印返回的行数据
        console.log(result.rows);    
      });
  });

function doRelease(connection)
{
  connection.close(
    function(err) {
      if (err) {
        console.error(err.message);
      }
    });
}

然后执行 node xx.js查看结果

四 约束

node_oracledb支持以下各种格式:
blob,buffer,clob,cursor,date,default,number,string。
明显,对于GIS中的oraclespatial数据类型不直接支持:

NJS-010: unsupported data type in select list

可以考虑将geom转wkt,gml等输出查询。



作者:遥想公瑾当年
链接:http://www.jianshu.com/p/83833b86eeca
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://my.oschina.net/huqiji/blog/1544259

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值