![](/img/trans.png)
[英]how to create rest api using node.js express js and oracledb for update database
[英]Node.js Oracledb executeMany() fail to update database
我正在使用 oracledb package for NodeJs 通過使用 executeMany() 在數據庫中執行多次更新,不幸的是每次我嘗試更新時都會收到以下錯誤:ORA-01722: invalid number 。
來自數據庫的數據類型:來自數據庫的數據類型
樣本數據 存在於數據庫樣本數據
Output 錯誤:錯誤
Oracle數據庫版本為11g
我在哪里做錯了。?? 請幫忙。
這是我的示例代碼片段。
oracledb.getConnection(connAttrs_, function (err, connection) {
if (err) {
//Error connecting to DB
res.set('Content-Type', 'application/json');
res.status(500).send(JSON.stringify({
status: 500,
message: "Error connecting to DB",
detailed_message: err.message
}));
console.log(err.message);
return;
}
var sql = "UPDATE tblTestBulkUpdate SET NAME =:2 WHERE TO_NUMBER(ID) =:1";
var binds = [
[10, "updated"],
[11, "updated two"],
[20, "why this"],
[22, "dummy data"],
[30, "ok ok"],
[40, "fig zero"],
[45, "fig five"],
[47, "fig seven"],
];
var options = {
autoCommit: true,
batchErrors: true,
bindDefs: [
{ type: oracledb.NUMBER},
{ type: oracledb.STRING, maxSize: 50}
]
};
connection.executeMany(sql, binds, options, function (err, result) {
if (err)
console.error(err);
else {
console.log("Result is:", result);
}
});
});
使用如下語法:
var binds = [
[10, "updated"],
...
是'bind by position ',而不是'bind by number ',所以10映射到語句中解析的第一個綁定變量,恰好被稱為“:2”。 請參閱說明的文檔
數組值的 position 對應於出現在語句中的 SQL 綁定參數的 position,無論它們的名稱如何。 即使綁定參數被命名為:0、:1 等,這仍然是正確的。
您可以嘗試這樣的片段,這將起作用,因為binds
變量首先具有字符串:
sql = `select * from dual where 'abc' = :2 and 123 = :1`;
binds = ['abc', 123];
result = await connection.execute(sql, binds);
如果您無法更改數據順序,請嘗試使用按名稱綁定語法。
在em_insert1.js中有一個按名稱綁定語法的示例:
const sql = "INSERT INTO no_em_tab values (:a, :b)";
const binds = [
{ a: 1, b: "Test 1 (One)" },
{ a: 2, b: "Test 2 (Two)" },
{ a: 3, b: "Test 3 (Three)" },
{ a: 4 },
{ a: 5, b: "Test 5 (Five)" }
];
// bindDefs is optional for IN binds but it is generally recommended.
// Without it the data must be scanned to find sizes and types.
const options = {
autoCommit: true,
bindDefs: {
a: { type: oracledb.NUMBER },
b: { type: oracledb.STRING, maxSize: 15 }
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.