操作系统: Windows8 64
node-module: node-oracle(https://github.com/joeferner/node-oracle)
安装过程中如果出现找不到 v8.h 之类的错误,请升级node到最新版!
1.安装node
node官网(http://nodejs.org/download/)下载 node-v0.10.28-x64.msi (http://nodejs.org/dist/v0.10.28/x64/node-v0.10.28-x64.msi)安装;
2.安装 Oracle instant client(http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html)
Oracle instant client 是 Oracle 提供的访问数据库的 C++接口, 下载windows 64位(http://www.oracle.com/technetwork/topics/winx64soft-089540.html) Version 12.1.0.1.0 中的 instantclient-basic-windows.x64-12.1.0.1.0.zip和instantclient-sdk-windows.x64-12.1.0.1.0.zip; 并把它们解压到 C:\instantclient_12_1 文件夹中, 由于两个 zip 中的文件各不相同, 可以合并到同一个文件夹.
3.安装vs2012/2010
4.安装Python2.x
5.设置系统环境变量
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
注意: 对于2中下载的两个 zip 文件, OCI_VERSION 应设置为 12, 如果设置成 11, 则安装时会报错: LINK : fatal error LNK1181: cannot open input file 'C:\instantclient_12_1\sdk\lib\msvc\vc11\oraocci11.lib' ;
请确保 Path 中 c:\instantclient_12_1\vc11 在 c:\instantclient_12_1 前面. 如果Path中存在其他版本的oracle client配置,请保证 "c:\instantclient_12_1\vc11;c:\instantclient_12_1" 在 oracle client前.
6.安装 node-oracle
npm install oracle -g
(使用-g安装为全局modules,如果在某个project下使用,可以在project目录中实行 npm link oracle)
成功安装后会显示 node-oracle 的版本号及实际安装到的目录.
7.测试:
建表:
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);
查询:select.js
var oracle = require('oracle');
var connectData = {"hostname": "localhost", "database": "XE" ,
"user": "用户名", "password": "密码"};
oracle.connect(connectData, function(err, connection) {
if ( err ) {
console.log(err);
} else {
connection.execute("SELECT * FROM NODE_TEST", [], function(err, results) {
if ( err ) {
console.log(err);
} else {
console.log(results);
}
connection.close();
});
}
});
执行
新增:insert.js
var oracle = require('oracle');
var connectData = {"hostname": "localhost", "database": "XE" ,
"user": "用户名", "password": "密码"};
oracle.connect(connectData, function(err, connection) {
if ( err ) {
console.log(err);
} else {
connection.execute(" INSERT INTO NODE_TEST (A, B) " +
" VALUES (:1, :2) RETURNING A INTO :3 ",
['5', '6', new oracle.OutParam()],
function(err, results) {
if ( err ) {
console.log(err)
} else {
console.log(results);
}
connection.close();
}
);
}
});
执行
删除:delete.js
var oracle = require('oracle');
var connectData = {"hostname": "localhost", "database": "XE" ,
"user": "用户名", "password": "密码"};
oracle.connect(connectData, function(err, connection) {
if ( err ) {
console.log(err);
} else {
connection.execute("DELETE FROM NODE_TEST WHERE A = :1",
[5],
function(err, results) {
if ( err ) {
console.log(err);
} else {
console.log(results);
}
connection.close();
});
}
});
执行
更新:update.js
var oracle = require('oracle');
var connectData = {"hostname": "localhost", "database": "XE" ,
"user": "用户名", "password": "密码"};
oracle.connect(connectData, function(err, connection) {
if ( err ) {
console.log(err);
} else {
connection.execute("UPDATE NODE_TEST SET B = :1 WHERE A = :2",
[8, 3],
function(err, results) {
if ( err ) {
console.log(err);
} else {
console.log(results);
}
connection.close();
});
}
});
执行
DDL:
var oracle = require('oracle');
var connectData = {"hostname": "localhost", "database": "XE" ,
"user": "用户名", "password": "密码"};
oracle.connect(connectData, function(err, connection) {
if ( err ) {
console.log(err);
} else {
connection.execute("ALTER TABLE NODE_TEST ADD (C INT)",
[],
function(err, results) {
if ( err ) {
console.log(err);
} else {
console.log(results);
}
connection.close();
});
}
});
执行