4

我有一个文件。我想将其内容放入我的 oracle 数据库中的 blob 列或放入我的 PL/SQL 程序中的 blob 变量中。最好的方法是什么?

4

2 回答 2

15

要完全在 PL/SQL 中完成,该文件需要在服务器上,位于您需要在数据库中定义的目录中。创建以下对象:

CREATE OR REPLACE DIRECTORY
    BLOB_DIR
    AS
    '/oracle/base/lobs'
/



CREATE OR REPLACE PROCEDURE BLOB_LOAD
AS

    lBlob  BLOB;
    lFile  BFILE := BFILENAME('BLOB_DIR', 'filename');

BEGIN

    INSERT INTO table (id, your_blob)
        VALUES (xxx, empty_blob())
        RETURNING your_blob INTO lBlob;

    DBMS_LOB.OPEN(lFile, DBMS_LOB.LOB_READONLY);

    DBMS_LOB.OPEN(lBlob, DBMS_LOB.LOB_READWRITE);

    DBMS_LOB.LOADFROMFILE(DEST_LOB => lBlob,
                          SRC_LOB  => lFile,
                          AMOUNT   => DBMS_LOB.GETLENGTH(lFile));

    DBMS_LOB.CLOSE(lFile);
    DBMS_LOB.CLOSE(lBlob);

    COMMIT;

END;
/
于 2008-09-23T19:00:25.703 回答
0

取决于你的环境。在Java中你可以这样做......

// Need as OracleConnection in mConnection

// Set an EMPTY_BLOB()
String update = "UPDATE tablename"+
                " SET   blob_column = EMPTY_BLOB()"+
                " WHERE  ID = "+id;
CallableStatement stmt = mConnection.prepareCall(update);
stmt.executeUpdate();

// Lock the row FOR UPDATE
String select    = "BEGIN " +
                        "  SELECT " + blob_column
                        "  INTO ? " +
                        "  FROM " + tablename +
                        "  WHERE  ID = '" + id + "'" +
                        "  FOR UPDATE; " +
                        "END;";

stmt = mConnection.prepareCall(select);
stmt.registerOutParameter(1, java.sql.Types.BLOB);
stmt.executeUpdate();

BLOB blob = (BLOB) stmt.getBlob(1);
OutputStream bos = blob.setBinaryStream(0L);
FileInputStream fis = new FileInputStream(file);
//  Code needed here to copy one stream to the other
fis.close();
bos.close();
stmt.close();

mConnection.commit();

但这真的取决于您使用的环境/工具。需要更多信息。

于 2008-09-23T18:55:48.210 回答