【问题标题】:Error trying to insert image to BLOB type in Oracle database using node-oracledb尝试使用 node-oracledb 在 Oracle 数据库中将图像插入 BLOB 类型时出错
【发布时间】:2021-06-01 02:14:41
【问题描述】:

this answer 之后,我试图将 .jpg 文件保存到表内的 BLOB 类型列中。这是我目前所拥有的:

我创建了一个测试表:

CREATE TABLE test_lob(
    ID NUMBER
  , C  CLOB
  , B  BLOB
);

然后我写了这段代码:

async function getFile() {
  var str = fs.readFileSync('/path/to/image', 'utf8');
  await insertBlob(str);
}
...
async function insertBlob(str) {
  var connection;

  try {
    connection = await oracledb.getConnection({
      user:          process.env.USER,
      password:      process.env.PASS,
      connectString: process.env.SERVER_CONNECT_STRING
    });

    const result = await connection.execute(
      `
        INSERT INTO test_lob(id, b) VALUES(1, :b)
      `,
      { b: str },
      { autoCommit: true }
    );

    console.log(result);
  } catch(err) {
    console.error(err);
  } finally {
    if(connection) {
      try {
        await connection.close();
      } catch(err) {
        console.error(err);
      }
    }
  }
}

但我总是得到错误:ORA-01461:只能绑定 LONG 值以插入 LONG 列。我错过了什么?

【问题讨论】:

    标签: node.js oracle node-oracledb


    【解决方案1】:

    您将图像文件视为 Unicode 文本字符串。这是不正确的;您应该将其作为缓冲区读取。

     var imageBuffer = fs.readFileSync('/path/to/image');
      await insertBlob(imageBuffer);
    

    我相信 Oracle 会抱怨是因为您尝试将 Unicode 字符串插入二进制列。尝试插入缓冲区。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多