1.开始

Node.js:https://nodejs.org

OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTALL.md#instwin

      https://github.com/oracle/node-oracledb/blob/master/doc/api.md#resultsethandling

2.OracleDB安装

  下载安装即可,略

  • C Compiler with support for C++ 11 (Xcode, gcc, Visual Studio or similar)

  打开Visual Studio的安装文件,查看C编译器是否安装,见下图

  • The small, free Oracle Instant Client "basic" and "SDK" packages if your database is remote. Or use a locally installed database such as the free Oracle XE release

  打开Oracle Instant Client,免费下载basic和sdk两个压缩包,绿色软件无须安装

  instantclient-basic-windows.x64-12.1.0.2.0.zip  69MB

  instantclient-sdk-windows.x64-12.1.0.2.0.zip   2.62MB

  将两个ZIP文件解压到同一个目录中Z:\Softs\OracleClient\12GX64

  • Set OCI_LIB_DIR and OCI_INC_DIR during installation if the Oracle libraries and headers are in a non-default location

  打开我的电脑->属性->高级属性->环境变量,新增两个环境变量ORACLE_HOME64,OCI_LIB_DIR 和 OCI_INV_DIR

  ORACLE_HOME64 : Z:\Softs\OracleClient\12GX64

  OCI_LIB_DIR : %ORACLE_HOME64%\sdk\lib\msvc

  OCI_INV_DIR : %ORACLE_HOME64%\sdk\include

  将Z:\Softs\OracleClient\12GX64这个路径%ORACLE_HOME64%加到Path中。

  • 执行CMD命令
1
$ npm install oracledb

3.OracleDB普通查询

dbconfig.js 配置数据库连接字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = {
  user          : process.env.NODE_ORACLEDB_USER || "test",
 
  // Instead of hard coding the password, consider prompting for it,
  // passing it in an environment variable via process.env, or using
  // External Authentication.
  password      : process.env.NODE_ORACLEDB_PASSWORD || "test",
 
  // For information on connection strings see:
  // https://github.com/oracle/node-oracledb/blob/master/doc/api.md#connectionstrings
  connectString : process.env.NODE_ORACLEDB_CONNECTIONSTRING || "192.168.1.100/orcl",
 
  // Setting externalAuth is optional.  It defaults to false.  See:
  // https://github.com/oracle/node-oracledb/blob/master/doc/api.md#extauth
  externalAuth  : process.env.NODE_ORACLEDB_EXTERNALAUTH ? true false
};

app.js执行一个简单的查询语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
 
//打开一个链接
oracledb.getConnection(
  {
    user          : dbConfig.user,
    password      : dbConfig.password,
    connectString : dbConfig.connectString
  },
  function(err, connection)
  {
    if (err) {
      console.error(err.message);
      return;
    }
    //执行查询语句
    connection.execute(
      "SELECT department_id, department_name " +
        "FROM departments " +
        "WHERE manager_id < :id",
      [110],  // bind value for :id
      { maxRows: 10 },  // a maximum of 10 rows will be returned.  Default limit is 100
      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);
      }
    });
}

  

4.OracleDB ResultSet查询

   普通查询,默认最大返回100条数据,如果需要查询更多的数据那么需要建立一个DataReader和数据库保持连接然后一行一行的读取数据,这个在nodejs oracledb里面就叫ResultSet查询。

你可以这样使用ResultSet,每次返回一行数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
connection.execute(
  "SELECT employee_id, last_name FROM employees ORDER BY employee_id",
  [], // no bind variables
  { resultSet: true }, // return a result set.  Default is false
  function(err, result)
  {
    if (err) { . . . }
    fetchOneRowFromRS(connection, result.resultSet);
  });
});
 
. . .
 
function fetchOneRowFromRS(connection, resultSet)
{
  resultSet.getRow( // get one row
    function (err, row)
    {
      if (err) {
         . . .           // close the result set and release the connection
      else if (!row) { // no rows, or no more rows
        . . .            // close the result set and release the connection
      else {
        console.log(row);
        fetchOneRowFromRS(connection, resultSet);  // get next row
      }
    });
}

当然也可以每次返回多行数据,请使用numRows参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var numRows = 10;  // number of rows to return from each call to getRows()
 
connection.execute(
  "SELECT employee_id, last_name FROM employees ORDER BY employee_id",
  [], // no bind variables
  { resultSet: true }, // return a result set.  Default is false
  function(err, result)
  {
    if (err) { . . . }
    fetchRowsFromRS(connection, result.resultSet, numRows);
  });
});
 
. . .
 
function fetchRowsFromRS(connection, resultSet, numRows)
{
  resultSet.getRows( // get numRows rows
    numRows,
    function (err, rows)
    {
      if (err) {
         . . .                        // close the result set and release the connection
      else if (rows.length == 0) {  // no rows, or no more rows
        . . .                         // close the result set and release the connection
      else if (rows.length > 0) {
        console.log(rows);
        fetchRowsFromRS(connection, resultSet, numRows);  // get next set of rows
      }
    });
}

http://www.cnblogs.com/mengkzhaoyun/p/5405912.html

crossplatform---Nodejs in Visual Studio Code 07.学习Oracle的更多相关文章

  1. Nodejs in Visual Studio Code 07.学习Oracle

    1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...

  2. Nodejs in Visual Studio Code 02.学习Nodejs

    1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...

  3. Nodejs in Visual Studio Code 03.学习Express

    1.开始 下载源码:https://github.com/sayar/NodeMVA Express组件:npm install express -g(全局安装) 2.ExpressRest 打开目录 ...

  4. Nodejs in Visual Studio Code 14.IISNode与IIS7.x

    1.开始 部署IISNode环境请参考:Nodejs in Visual Studio Code 08.IIS 部署Nodejs程序请参考:Nodejs in Visual Studio Code 1 ...

  5. Nodejs in Visual Studio Code 11.前端工程优化

    1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...

  6. Nodejs in Visual Studio Code 10.IISNode

    1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...

  7. Nodejs in Visual Studio Code 04.Swig模版

    1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...

  8. Nodejs in Visual Studio Code 01.简单介绍Nodejs

    1.开始 作者自己:开发人员,Asp.Net , html / js , restful , memcached , oracle ,windows , iis 目标读者:供自己以后回顾 2.我看No ...

  9. crossplatform---Nodejs in Visual Studio Code 02.学习Nodejs

    1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...

随机推荐

  1. MapGIS6.7安装图文教程(完美破解)

    mapgis安装比较简单,主要注意在安装的时候,先打开软件狗,然后再进行软件安装,一般就不会照成其他安装失败的现象,有时候安装之前没有打开软件狗也安装成功了,也有这情况,不过软件使用也需要软件狗的支持 ...

  2. storyboard xib下label怎么自适应宽度高度

    先看需求:两个Label,要求蓝色的label紧跟在红色的label文字后面  ok首选正常添加约束 红色的Label添加宽度,高度,左边,上边约束 蓝色的Label添加宽度,高度,左边,和红色的水平 ...

  3. AJAX向服务器发送请求

    使用 XMLHttpRequest 对象的 open() 和 send() 方法: 方法 描述 open(method,url,async) 规定请求的类型.URL 以及是否异步处理请求. metho ...

  4. c# SqlHelper Class

    using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...

  5. Selenium2+python自动化19-单选框和复选框(radiobox、checkbox)

    本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...

  6. Selenium2+python自动化5-操作浏览器基本方法

    前言 前面已经把环境搭建好了,这从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可视化工具,我们要学的是 ...

  7. iOS大神牛人的博客集合

    王巍的博客:王巍目前在日本横滨任职于LINE.工作内容主要进行Unity3D开发,8小时之外经常进行iOS/Mac开发.他的陈列柜中已有多款应用,其中番茄工作法工具非常棒. http://onevca ...

  8. 第七章 springboot + retrofit

    retrofit:一套RESTful架构的Android(Java)客户端实现. 好处: 基于注解 提供JSON to POJO,POJO to JSON,网络请求(POST,GET,PUT,DELE ...

  9. delphi 10.1 berlin最新的开发框架:咏南中间件+咏南开发框架,购买后提供全部的源码

    咏南中间件+咏南开发框架支持最新的delphi 10.1(berlin),老用户提供免费升级. 购买提供:中间件源码 附带福利(赠送): CS开发框架源码BS开发框架源码移动APP源码中间件集群源码二 ...

  10. sqlite query用法

    本文转自http://blog.csdn.net/double2hao/article/details/50281273,在此感谢作者 query(table, columns, selection, ...