如何使用节点js将多个logging插入到oracle数据库中

我能够插入一个logging到一个表中,但我想一次插入多个logging到表中,

我的代码是低于 –

var doinsert_autocommit = function (conn, cb) { var query="INSERT INTO test VALUES (:id,:name)"; var values=[{1,'rate'},{5,'ratee'}]; 

如果我使用[1,'老鼠'] – 它的工作插入一行。

 conn.execute( "INSERT INTO test VALUES (:id,:name)", values, // Bind values { autoCommit: true}, // Override the default non-autocommit behavior function(err, result) { if (err) { return cb(err, conn); } else { console.log("Rows inserted: " + result.rowsAffected); // 1 return cb(null, conn); } }); 

};

目前,驱动程序只支持与PL / SQL的数组绑定,而不是直接的SQL。 我们希望在将来改善这一点。 现在,你可以做以下…

鉴于此表:

 create table things ( id number not null, name varchar2(50) not null ) / 

以下应该工作:

 var oracledb = require('oracledb'); var config = require('./dbconfig'); var things = []; var idx; function getThings(count) { var things = []; for (idx = 0; idx < count; idx += 1) { things[idx] = { id: idx, name: "Thing number " + idx }; } return things; } // Imagine the 'things' were fetched via a REST call or from a file. // We end up with an array of things we want to insert. things = getThings(500); oracledb.getConnection(config, function(err, conn) { var ids = []; var names = []; var start = Date.now(); if (err) {throw err;} for (idx = 0; idx < things.length; idx += 1) { ids.push(things[idx].id); names.push(things[idx].name); } conn.execute( ` declare type number_aat is table of number index by pls_integer; type varchar2_aat is table of varchar2(50) index by pls_integer; l_ids number_aat := :ids; l_names varchar2_aat := :names; begin forall x in l_ids.first .. l_ids.last insert into things (id, name) values (l_ids(x), l_names(x)); end;`, { ids: { type: oracledb.NUMBER, dir: oracledb.BIND_IN, val: ids }, names: { type: oracledb.STRING, dir: oracledb.BIND_IN, val: names } }, { autoCommit: true }, function(err) { if (err) {console.log(err); return;} console.log('Success. Inserted ' + things.length + ' rows in ' + (Date.now() - start) + ' ms.'); } ); }); 

这将向数据库中单次往返插入500行。 另外,在DB中的SQL和PL / SQL引擎之间切换一个上下文。

正如你所看到的,数组必须单独绑定(不能绑定一个数组对象)。 这就是为什么这个例子演示了如何将它们分解成单独的数组以达到绑定的目的。 随着时间的推移,这应该会变得更加优雅,但是现在这个效果已经很好

我使用了简单的oracledb库来批量插入,它扩展了oracledb模块。

 var async = require('async'); var oracledb = require('oracledb'); var dbConfig = require('./dbconfig.js'); var SimpleOracleDB = require('simple-oracledb'); SimpleOracleDB.extend(oracledb); var doconnect = function(cb) { oracledb.getConnection( { user : dbConfig.user, password : dbConfig.password, connectString : dbConfig.connectString }, cb); }; var dorelease = function(conn) { conn.close(function (err) { if (err) console.error(err.message); }); }; var doinsert_autocommit = function (conn, cb) { conn.batchInsert( "INSERT INTO test VALUES (:id,:name)", [{id:1,name:'nayan'},{id:2,name:'chaan'},{id:3,name:'man'}], // Bind values { autoCommit: true}, // Override the default non-autocommit behavior function(err, result) { if (err) { return cb(err, conn); } else { console.log("Rows inserted: " + result.rowsAffected); // 1 return cb(null, conn); } }); }; async.waterfall( [ doconnect, doinsert_autocommit, ], function (err, conn) { if (err) { console.error("In waterfall error cb: ==>", err, "<=="); } if (conn) dorelease(conn); });