尝试使用 node-oracledb 将图像插入 Oracle 数据库中的 BLOB 类型时出错

Error trying to insert image to BLOB type in Oracle database using node-oracledb

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

我创建了一个测试 table:

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 列。我错过了什么?

您阅读图像文件时就好像它是 Unicode 文本字符串一样。那是不正确的;你应该把它当作一个缓冲区来读。

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

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