gpt4 book ai didi

javascript - 在 NodeJS 中将数据插入到 oracle 数据库表中

转载 作者:行者123 更新时间:2023-12-02 23:00:18 25 4
gpt4 key购买 nike

我想使用下面代码中引用的 insertIntoTable 将数据插入到 Oracle 数据库表中;该函数不起作用,但如果没有它,查询将按预期工作。

let connection;
var oracledb = require('oracledb');

(async function() {
try{

connection = await oracledb.getConnection({
user : 'demo7',
password : 'dbpass',
connectString : 'localhost/induspdb'
});
console.log("Successfully connected to Oracle!");

//function which insert result into table
async function insertIntoTable(dateToday, fileFound, fileNotFound )
{
const query='insert into backupinfo (infdate,found,notfound) values (:1,:2,:3)';
var binds=[dateToday,fileFound,fileNotFound];
await connection.execute(query , binds, {autoCommit:true});
}

// module.exports.insertIntoTable=insertIntoTable;
insertIntoTable('2019-09-06','rtx','agh');

} catch(err) {
console.log("Error: ", err);
} finally {
if (connection) {
try {
await connection.close();
} catch(err) {
console.log("Error when closing the database connection: ", err);
}
}
}

})()

每当我调用此函数并传递给定参数时,此函数都应将值插入表中,这是错误输出:

Successfully connected to Oracle!

(node:10088) UnhandledPromiseRejectionWarning: Error: DPI-1010: not connected
(node:10088) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an as
ync function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10088) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are n
ot handled will terminate the Node.js process with a non-zero exit code.

最佳答案

根据数据库中的表类型检查绑定(bind)的所有参数是否有效。另外,在insertIntoTable前加上“await”,这样报错就不会是UnhandledPromiseRejection了,会更清晰。

关于javascript - 在 NodeJS 中将数据插入到 oracle 数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57819823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com