我有一个档案。我想将其内容放入我的oracle数据库中的blob列或PL / SQL程序中的blob变量中。最好的方法是什么?
答案 0 :(得分: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;
/
答案 1 :(得分: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();
但这实际上取决于您使用的环境/工具。需要更多信息。