Nodejs 入门4 使用Oracle数据库
- 一、安装驱动
- 设置环境变量
- 二、示例代码
一、安装驱动
1. 下载代码
https://github.com/oracle/node-oracledb 把代码clone到本地
2. ubuntu下安装
(1) 下载:Instant Client
http://www.oracle.com/technetwork/database/features/instant-client/index-100365.html
- oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
- oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
- oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
(2) 运行命令转换格式并安装:
转换格式
sudo alien -k oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
sudo alien -k oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
sudo alien -k oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
安装
sudo dpkg -i oracle-instantclient12.1-basic_12.1.0.2.0-1_amd64.deb
sudo dpkg -i oracle-instantclient12.1-devel_12.1.0.2.0-1_amd64.deb
sudo dpkg -i oracle-instantclient12.1-sqlplus_12.1.0.2.0-1_amd64.deb
设置环境变量
sudo gedit /etc/profile
export ORACLE_HOME=/usr/lib/oracle/11.2/client
export ORACLE_BASE=/usr/lib/oracle/11.2
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
生效
source /etc/profile
sudo reboot
其它依赖的包
apt-get install libaio-dev #centos使用 yum install libaio
运行sqlplus
/usr/lib/oracle/12.1/client64/bin/sqlplus
环境变量
vim /root/.bash_profile
最后加入:
LD_LIBRARY_PATH=/usr/lib/oracle/11.1/client64
运行命令:
sudo -s
source /root/.bash_profile
把 /opt/oracle/instantclient_12_1 设置进环境变量:LD_LIBRARY_PATH 。
export LD_LIBRARY_PATH="/opt/oracle/instantclient_12_1"
npm install
或者直接运行:
npm install oracledb
##win7下编译
###下载Oracle 11g Express
这是Oracle Express免费版本。也许不装它,使用instantclient也可以,我没有尝试。
###安装vs2012 Express
###安装Nodejs
###安装InstantClient
####下载
instantclient-basic-windows.x64-12.1.0.2.0.zip
instantclient-sdk-windows.x64-12.1.0.2.0.zip
####解压后放到:
C:\instantclient_12_1
现在它显示是这样的:
设置环境变量
OCI_INCLUDE_DIR=C:\instantclient_12_1\sdk\include
OCI_LIB_DIR=C:\instantclient_12_1\sdk\lib\msvc\vc11
OCI_VERSION=12
Path=...;c:\instantclient_12_1\vc11;c:\instantclient_12_1
这里有个编译好的项目:
https://github.com/bmulcahy/node-oracledb_selfcontained
二、示例代码
var oracledb = require('oracledb');
oracledb.getConnection(
{
user : "hr",
password : "welcome",
connectString : "localhost/XE"
},
function(err, connection)
{
if (err) {
console.error(err.message);
return;
}
connection.execute(
"SELECT department_id, department_name "
+ "FROM departments "
+ "WHERE department_id = :did",
[180],
function(err, result)
{
if (err) {
console.error(err.message);
return;
}
console.log(result.rows);
});
});
参考:
http://blog.gaoqixhb.com/p/53d98e7f3cff58870566e9b4