目標
- Node.jsでOracleを使用してCRUD処理をする
今回は「Oracle Instant Client」と、oracle公式が提供するNodeのパッケージである「oracledb」を使用する
※osはwindowsです
Oracle Instant Client
Oracle Instant Client basicのダウンロード
まずは以下のサイトから「Oracle Instant Client basic」をダウンロードします。画像の赤線で囲んだ部分をクリック
Oracle Instant Client Downloads
SDK Packageのダウンロード
上でダウンロードしたものの少し下にある「SDK Package」をダウンロード
次に、ダウンロードした2つを同じ場所に展開。統合されて以下のようになるはず。
※今回はDドライブ直下に展開した
環境変数のセット
コマンドラインを立ち上げ、以下のコマンドを実行
※パスは instant clientを展開した場所に合わせる
1 2 3 |
set PATH= D:\instantclient_19_12;%PATH% set OCI_LIB_DIR=D:\instantclient_19_12\sdk\lib\msvc set OCI_INC_DIR=D:\instantclient_19_12\sdk\include |
環境変数の設定ができているかを確認するため、以下のコマンドを実行。oci.h ファイルが開ければok
1 |
notepad %OCI_INC_DIR%\oci.h |
Oracledb のインストール
コマンドラインからnpmでインストールする
nodeのバージョン確認
以下のサイトで、oracledbのバージョンと、使用しているnodeのバージョンが対応していることを確認
oracledb のインストール
今回は v5.1.0をインストールした
1 |
npm install oracledb@5.1.0 |
Node.js のサンプルコード
注意
●テーブル名・カラム名に小文字を使用しているとエラーが出る(oracledb:v5.1.0 node:v10.16.0 で確認)
小文字が使用された列名を参照しようとすると以下のエラー。SQLを小文字で書いてもダメ。
おそらく、oracledbが小文字に対応しておらず、小文字でSQLを書いても大文字として認識してしまうため。
1 |
{ [Error: ORA-00904: "DATE_": 無効な識別子です。] errorNum: 904, offset: 23 } |
従って、oracledbで扱うテーブル名・カラム名は全て大文字で定義すること
接続・テーブル削除・作成
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 47 48 49 50 51 |
const express = require('express'); const router = express.Router(); /* oracledb の読み込み */ const oracledb = require('oracledb'); /* oracledb の接続設定 */ const dbConfig = { user: "ユーザー名", password: "パスワード", connectionString: "DBのパス" } router.get('/', (req, res, next) => { (async function () { var connection; try { /* DB接続 */ connection = await oracledb.getConnection(dbConfig); /* test1テーブルが存在する場合、テーブル削除 */ await connection.execute(` begin execute immediate 'drop table test1'; exception when others then if sqlcode <> -942 then raise; end if; end;`); /* テーブル作成。小文字で指定しても、作成されるテーブル名・カラム名は大文字になる */ await connection.execute(` create table test1 ( id number generated always as identity, description varchar2(4000), creation_ts timestamp with time zone default current_timestamp, done number(1,0), primary key (id))`); } catch (err) { console.error(err); } finally { if (connection) { try { await connection.close(); } catch (err) { console.error(err); } } } })(); }); module.exports = router; |
Create (insert)
※前項と共通する部分( try{} の外)は省略しています
1 2 3 4 5 6 7 8 9 10 |
/* DB接続 */ connection = await oracledb.getConnection(dbConfig); /* SQL文とパラメータ。:1, :2 には 配列row の値が入る */ const sql = `INSERT INTO test1 (str_, num_) values(:1, :2)` const row = ["str1", 1] /* SQLを実行。INSERT, UPDATE, DELETE は実行後に commit が必要 */ await connection.execute(sql, row); await connection.commit(); |
Read (select)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* DB接続 */ connection = await oracledb.getConnection(dbConfig); /* 実行する SQL を設定。SQL文は小文字でも構わない */ var sql = `select * from test1` /* SQLを実行 */ var result = await connection.execute(sql, [], { outFormat: oracledb.OUT_FORMAT_OBJECT }); /* 結果を出力 */ var rows = result.rows; for (var i in rows) { console.log(rows[i]); } |
Update
1 2 3 4 5 6 7 8 9 10 |
/* DB接続 */ connection = await oracledb.getConnection(dbConfig); /* SQL文とパラメータ。:1, :2 には 配列row の値が入る */ const sql = `UPDATE test1 SET str_ = :1, num_ = :2 where id = 1` const row = ["str2", 2] /* SQLを実行。INSERT, UPDATE, DELETE は実行後に commit が必要 */ await connection.execute(sql, row); await connection.commit(); |
Delete
1 2 3 4 5 6 7 8 9 |
/* DB接続 */ connection = await oracledb.getConnection(dbConfig); /* SQL文 */ const sql = `DELETE FROM test1 where id = 1` /* SQLを実行。INSERT, UPDATE, DELETE は実行後に commit が必要 */ await connection.execute(sql); await connection.commit(); |