forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample.js
84 lines (64 loc) · 1.99 KB
/
Example.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*----------------------------------------------------------------------
Example.js
Demonstrate how to perform a database insert and query with Node.js in
Oracle Database Cloud services such as Exadata Express, Autonomous
Transaction Processing, Autonomous Data Warehouse, and others.
Before running this script:
- Install Node.js and node-oracledb
- Install Oracle Instant Client
- Download and install the cloud service wallet
- Modify the getConnection() call below to use the credentials for your database.
See your cloud service's documentation for details.
----------------------------------------------------------------------*/
var oracledb = require('oracledb');
async function run() {
let connection;
try {
let sql, binds, options, result;
connection = await oracledb.getConnection( {
user : 'username',
password : 'password',
connectString : 'connect_string'
});
// Create a table
await connection.execute(
`BEGIN
EXECUTE IMMEDIATE 'DROP TABLE mycloudtab';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE NOT IN (-00942) THEN
RAISE;
END IF;
END;`);
await connection.execute(
`CREATE TABLE mycloudtab (id NUMBER, data VARCHAR2(20))`);
// Insert some data
sql = `INSERT INTO mycloudtab VALUES (:1, :2)`;
binds = [ [101, "Alpha" ], [102, "Beta" ], [103, "Gamma" ] ];
options = {
autoCommit: true,
bindDefs: [
{ type: oracledb.NUMBER },
{ type: oracledb.STRING, maxSize: 20 }
]
};
await connection.executeMany(sql, binds, options);
// Query the data
sql = `SELECT * FROM mycloudtab`;
binds = {};
options = {};
result = await connection.execute(sql, binds, options);
console.log(result.rows);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();