NodeJs连接Oracle数据库(win7+64位)

 

1。安装instantclient(连接oracle的轻量级免安装包)

   下载链接:http://www.oracle.com/technetwork/topics/winx64soft-089540.html

   下载:

    1) instantclient-basiclite-windows.x64-12.2.0.1.0.zip

   2)  instantclient-sdk-windows.x64-12.2.0.1.0.zip

安装方法和设置环境变量请参考:

    1)  http://www.cnblogs.com/vipstone/p/4794747.html

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

注:主要是设置OCI_INC_DIR和OCI_LIB_DIR。 这里有个坑,PATH中需添加instantclient的路径(D:\oracle\instantclient_12_2),oracle官方文档中提及了,上述的1)中没说。如果PATH中没设置,后面会报错:

Error: The specified procedure could not be found.
\\?\C:\Users\JackYao\javascripting\node_modules\oracledb\build\Release\oracledb.node
    at Object.Module._extensions..node (module.js:598:18)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\JackYao\javascripting\node_modules\oracledb\lib\oracledb.js:35:19)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)

2。npm 安装oracledb。安装oracledb需要用node-gyp和visualstudio编译。这里比较繁琐。

   上述oracle官方文档用的visualstudio版本是Visual Studio 2013 Community Edition,本人根据node-gyp(https://github.com/nodejs/node-gyp)的文档:

On Windows:

  • Option 1: Install all the required tools and configurations using Microsoft's windows-build-tools using npm install --global --production windows-build-tools from an elevated PowerShell or CMD.exe (run as Administrator).
  • Option 2: Install tools and configuration manually:
    • Visual C++ Build Environment:

      • Option 1: Install Visual C++ Build Tools using the Default Install option.

      • Option 2: Install Visual Studio 2015 (or modify an existing installation) and select Common Tools for Visual C++ during setup. This also works with the free Community and Express for Desktop editions.

      :bulb: [Windows Vista / 7 only] requires .NET Framework 4.5.1

    • Install Python 2.7 (v3.x.x is not supported), and run npm config set python python2.7 (or see below for further instructions on specifying the proper Python version and path.)

    • Launch cmd, npm config set msvs_version 2015   

 即:

1) 安装Microsoft Windows的工具:

npm install --global windows-build-tools

该命令会自动安装 Visual C++ Build Tools 2015(另外还安装了python 2.7)。

注::bulb: [Windows Vista / 7 only] requires .NET Framework 4.5.1 (Currently not installed automatically by this package

2)  安装.NET Framework 4.5.1   (链接:https://www.microsoft.com/en-us/download/details.aspx?id=40773)

    用的是offline版本(NDP451-KB2858728-x86-x64-AllOS-ENU.exe  66.8M)

注:Visual C++ Build Tools 2015似乎还需要手工安装的。windows-build-tools虽然自动下载了BuildTools_Full.exe这个安装工具,但好像没有自动安装的。本人是手工点击这个工具安装的。安装Visual C++ Build Tools 2015需要1小时,占用硬盘超过3G。 崩溃!现在可以用npm install oracledb -g 安装oracledb了。

3)执行命令:npm install oracledb -g。成功会输出如下:

C:\Users\JackYao\javascripting>npm install oracledb
/
> oracledb@1.13.1 install C:\Users\JackYao\javascripting\node_modules\oracledb
> node-gyp rebuild


C:\Users\JackYao\javascripting\node_modules\oracledb>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\n
ode_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
  njsOracle.cpp
  njsPool.cpp
  njsConnection.cpp
  njsResultSet.cpp
  njsMessages.cpp
  njsIntLob.cpp
  dpiEnv.cpp
  dpiEnvImpl.cpp
  dpiException.cpp
  dpiExceptionImpl.cpp
  dpiConnImpl.cpp
  dpiDateTimeArrayImpl.cpp
  dpiPoolImpl.cpp
  dpiStmtImpl.cpp
  dpiUtils.cpp
  dpiLob.cpp
  dpiCommon.cpp
  win_delay_load_hook.c
     Creating library C:\Users\JackYao\javascripting\node_modules\oracledb\build\Release\oracledb.lib and object C:\Use
  rs\JackYao\javascripting\node_modules\oracledb\build\Release\oracledb.exp
  Generating code
  Finished generating code
  oracledb.vcxproj -> C:\Users\JackYao\javascripting\node_modules\oracledb\build\Release\\oracledb.node
  oracledb.vcxproj -> C:\Users\JackYao\javascripting\node_modules\oracledb\build\Release\oracledb.pdb (Full PDB)
oracledb@1.13.1 node_modules\oracledb
└── nan@2.5.1

C:\Users\JackYao\javascripting>node select.js
C:\Users\JackYao\javascripting\node_modules\oracledb\lib\oracledb.js:38
    throw err;
    ^

Error: The specified procedure could not be found.
\\?\C:\Users\JackYao\javascripting\node_modules\oracledb\build\Release\oracledb.node
    at Object.Module._extensions..node (module.js:598:18)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\JackYao\javascripting\node_modules\oracledb\lib\oracledb.js:35:19)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)

C:\Users\JackYao\javascripting>


注:这里node-gyp有个bug,一开始编译报错:__pfnDliNotifyHook2 重复定义。暴力解决:注释C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\delayimp.h文件的130~134行。

3。测试验证

   1)  安装 Oracle 11g express   详见: 《Windows 7 x64 安装 Oracle 11g Express》

   2) 创建test用户:

        create user test identified by test123;  #创建

       grant create session,create table,create view,create sequence,unlimited tablespace to test; #为用户分配权限

   3)  在 oracle 中创建 NODE_TEST 表并添加数据.         

 CREATE TABLE NODE_TEST ( A INTEGER, B INTEGER ); 

          INSERT INTO NODE_TEST(A, B) VALUES (1, 2); 

          INSERT INTO NODE_TEST(A, B) VALUES (3, 4);

   4) 在 C:\TEMP 目录下新建 select.js.        

var oracledb = require('oracledb');  
  
oracledb.getConnection({  
     user: "test",  
     password: "test123",  
     connectString: "localhost/xe"  
}, function(err, connection) {  
     if (err) {  
          console.error(err.message);  
          return;  
     }  
     connection.execute( "SELECT * FROM NODE_TEST",  
     [],  
     function(err, result) {  
          if (err) {  
               console.error(err.message);  
               doRelease(connection);  
               return;  
          }  
          console.log(result.metaData);  
          console.log(result.rows);  
          doRelease(connection);  
     });  
});  
  
function doRelease(connection) {  
     connection.release(  
          function(err) {  
               if (err) {console.error(err.message);}  
          }  
     );  
}  

5) 用node selects.js运行,输出

C:\Users\JackYao\javascripting>node select.js
[ { name: 'A' }, { name: 'B' } ]
[ [ 1, 2 ], [ 3, 4 ] ]

 

转载于:https://my.oschina.net/u/3184245/blog/912775

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值