简体   繁体   English

Node.js Oracledb executeMany() 更新数据库失败

[英]Node.js Oracledb executeMany() fail to update database

Im am Using oracledb package for NodeJs to perform multiple update in a database by using executeMany(), unfortunately each time i try to update i get the following Error: ORA-01722: invalid number .我正在使用 oracledb package for NodeJs 通过使用 executeMany() 在数据库中执行多次更新,不幸的是每次我尝试更新时都会收到以下错误:ORA-01722: invalid number

Data Types from Database: Data Types from Database来自数据库的数据类型:来自数据库的数据类型

Sample data Existing in Data base Sample Data样本数据 存在于数据库样本数据

Output Error: Error Output 错误:错误

Oracle database Version is 11g Oracle数据库版本为11g

Where am i doing it wrong.??我在哪里做错了。?? Help please.请帮忙。

Here is my Sample Code Snippet.这是我的示例代码片段。

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);
        }
      });
});

Using syntax like:使用如下语法:

var binds = [
        [10, "updated"],
...

is 'bind by position ', not 'bind by number ', so the 10 maps to the first bind variable parsed in the statement which happens to be called ":2".是'bind by position ',而不是'bind by number ',所以10映射到语句中解析的第一个绑定变量,恰好被称为“:2”。 See the doc which says请参阅说明的文档

The position of the array values corresponds to the position of the SQL bind parameters as they occur in the statement, regardless of their names.数组值的 position 对应于出现在语句中的 SQL 绑定参数的 position,无论它们的名称如何。 This is still true even if the bind parameters are named like:0, :1, etc.即使绑定参数被命名为:0、:1 等,这仍然是正确的。

You can experiment with snippets like this, which will work because the binds variable has the string first:您可以尝试这样的片段,这将起作用,因为binds变量首先具有字符串:

sql = `select * from dual where 'abc' = :2 and 123 = :1`;
binds = ['abc', 123];
result = await connection.execute(sql, binds);

If you can't change the data order, then try using bind by name syntax.如果您无法更改数据顺序,请尝试使用按名称绑定语法。

There is an example of bind-by-name syntax in em_insert1.js :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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM