Nodejs node-oracle 插入多条数据的时候 报错 有没有遇到的 求解决

Nodejs node-oracle 插入多条数据的时候 报错 有没有遇到的 求解决

node: /usr/include/nodejs/src/node_object_wrap.h:61: static T* node::ObjectWrap::Unwrap(v8::Handlev8::Object) [with T = OutParam]: Assertion `handle->InternalFieldCount() > 0’ failed. 已放弃 (核心已转储)

采用的就是这里的方法: https://github.com/joeferner/node-oracle#large-inserts-or-updates


3 回复

Node.js node-oracle 插入多条数据时报错问题

问题描述

在使用 node-oracle 插入多条数据时遇到了错误。具体错误信息为:

node: /usr/include/nodejs/src/node_object_wrap.h:61: static T* node::ObjectWrap::Unwrap(v8::Handle<v8::Object>) [with T = OutParam]: Assertion `handle->InternalFieldCount() > 0' failed.
已放弃 (核心已转储)

解决方案

该问题可能是由于参数传递或数据库连接配置不当导致的。以下是一个完整的示例代码,展示如何正确地使用 node-oracle 插入多条数据。

首先确保你已经安装了 oracledb 包(node-oracle 的替代品,更现代且活跃):

npm install oracledb

然后可以使用以下代码来插入多条数据:

const oracledb = require('oracledb');

async function insertMultipleRows() {
    let connection;

    try {
        // 连接到数据库
        connection = await oracledb.getConnection({
            user: 'your_username',
            password: 'your_password',
            connectString: 'your_connection_string'
        });

        // 开启事务
        await connection.execute('BEGIN DBMS_LOCK.SLEEP(2); END;');

        const rowsToInsert = [
            { column1: 'value1', column2: 'value2' },
            { column1: 'value3', column2: 'value4' }
        ];

        // 准备 SQL 语句
        const sql = `
            INSERT INTO your_table (column1, column2)
            VALUES (:1, :2)
        `;

        // 执行批量插入
        await connection.executeMany(sql, rowsToInsert);

        console.log('Data inserted successfully');
    } catch (err) {
        console.error(err);
    } finally {
        if (connection) {
            try {
                await connection.close();
            } catch (err) {
                console.error(err);
            }
        }
    }
}

insertMultipleRows().catch(console.error);

代码解释

  1. 连接数据库:使用 oracledb.getConnection() 方法连接到 Oracle 数据库。
  2. 开启事务:通过执行一个简单的 SQL 语句(如 DBMS_LOCK.SLEEP)来模拟事务处理。
  3. 准备数据:定义要插入的数据数组 rowsToInsert
  4. SQL 语句:使用 INSERT INTO 语句并使用绑定变量 :1, :2 来插入数据。
  5. 批量插入:使用 connection.executeMany() 方法来执行批量插入操作。
  6. 异常处理:在 try...catch 块中处理可能出现的错误,并在 finally 块中关闭数据库连接。

通过这种方式,你可以避免之前出现的错误,并成功插入多条数据。如果问题仍然存在,请检查你的数据库连接字符串、用户名和密码是否正确。


求大神解决这个问题。。

根据你描述的问题,错误信息与 node-oracle 的使用无关,而更可能与 Node.js 的 V8 引擎内部断言失败有关。然而,针对你的需求,即在使用 node-oracle 插入多条数据时遇到问题,我们可以提供一个正确的插入方法。

解决方案

为了插入多条数据,你可以构建一个包含多个绑定参数的数组,并一次性提交到数据库。以下是一个简单的示例代码:

const oracledb = require('oracledb');

async function insertMultipleRows(connection) {
    const sql = `INSERT INTO your_table_name (column1, column2) VALUES (:1, :2)`;

    // 示例数据
    const dataToInsert = [
        ['value1', 'value2'],
        ['value3', 'value4']
    ];

    try {
        await connection.executeMany(sql, dataToInsert);
        console.log("Data inserted successfully.");
    } catch (err) {
        console.error("Error inserting data:", err.message);
    }
}

async function main() {
    let connection;

    try {
        // 连接到数据库
        connection = await oracledb.getConnection({
            user: "your_username",
            password: "your_password",
            connectString: "your_connection_string"
        });

        await insertMultipleRows(connection);
    } catch (err) {
        console.error("Database connection or query error:", err.message);
    } finally {
        if (connection) {
            try {
                // 确保关闭连接
                await connection.close();
            } catch (err) {
                console.error("Error closing database connection:", err.message);
            }
        }
    }
}

main();

在这个示例中,我们使用了 executeMany() 方法来插入多条记录。确保你将 your_table_name, column1, column2, your_username, your_password, 和 your_connection_string 替换为实际值。

如果问题仍然存在,请检查数据库连接字符串、用户名和密码是否正确,并且确认数据库用户具有相应的写权限。此外,如果你使用的是 Oracle Instant Client 或其他依赖项,请确保它们安装正确且版本兼容。

回到顶部