gpt4 book ai didi

oracle - DBMS_LOB.LoadFromFile 完整性检查

转载 作者:行者123 更新时间:2023-12-01 14:00:52 25 4
gpt4 key购买 nike

我想也许我开始在这里得到这个过程,但它太奇怪了,我怀疑自己。下面看评论,有什么乱七八糟的地方。

DECLARE
srcFile BFILE := BFILENAME('SOME_DIR', 'xyz.pdf');
fLen NUMBER;
dest BLOB;

BEGIN
INSERT INTO SomeTable
VALUES ( 1, 'xyz.pdf', EMPTY_BLOB(), null ) -- Does this establish an active ref.?
RETURNING pdf_file INTO dest;

DBMS_LOB.OPEN(srcFile, dbms_lob.file_readonly);
fLen := dbms_lob.getlength(srcFile);

DBMS_LOB.LOADFROMFILE(dest, srcFile, fLen); -- Does this reach into the table row,
-- and fill it with the file’s contents?
dbms_lob.close(srcFile);
commit;
END;

下面的方法是更新已经存在的行中的 BLOB 吗?

DECLARE
srcFile BFILE := BFILENAME('SOME_DIR', 'xyz.pdf');
fLen NUMBER;
dest BLOB;

BEGIN
SELECT pdf_file INTO dest -- Does this est. an active reference?
FROM SomeTable
WHERE ID = 1; -- ( <———<<< ' = 1' is just for example.)

DBMS_LOB.OPEN(srcFile, dbms_lob.file_readonly);
fLen := dbms_lob.getlength(srcFile);

DBMS_LOB.LoadFromFile(dest, srcFile, fLen); -- Does this reach into the row,
-- filling it w/ the file’s contents?
dbms_lob.close(srcFile);
commit;
END;

这看起来更像是 .NET 对数据库适配器以及 FileInfo、DirInfo 函数的处理。但我在 Oracle 的其他任何地方都没有看到这种理念。

我早就料到了

  BLOBVariable = LoadFromFile(srcLocator, byteCnt); -- where the func. rtrns a val,

跟着

  INSERT INTO SomeTable (pdf_file)
VALUES (BLOBVariable);

我没看错吗?如果是这样,什么时候放弃引用?我似乎记得读过使用“LoadFromFile?”时不需要“提交”的内容?

现在,今天早上我看到了一个示例(没有解释),它选择 BLOB 字段到一个变量中,使用 FOR UPDATE 来锁定记录。了解这是怎么回事可能会很好......

    -- Lock the record
execute immediate 'SELECT pdf_file INTO dest
FROM image_blobs
WHERE itl_image_blob_id = :1
FOR UPDATE'
INTO v_blob_data
using < the row identifier goes here >;

-- Read the file
dbms_lob.loadfromfile(dests, srcFile, fLen);

-- update the blob field
execute immediate '
UPDATE image_blobs
SET pdf_file = :1
WHERE itl_image_blob_id = :2'
using dest, < row identifier >;

最佳答案

以下内容表明我的理智完好无损,INSERT 语句确实创建了 loadfromfile() 使用的引用。这个关于 INSERT 语句的答案足以满足我的目的,因此我将假设 UPDATE 功能是相似的。 'IMPORT_DIR' 是数据库中的命名数据库目录。

DROP TABLE TEST_BLOBS purge; -- 'purge' prevents loading up the recycle bin.
CREATE TABLE TEST_BLOBS (
ID VARCHAR(4),
F_NAME VARCHAR2(50 BYTE),
CONTENT_TYPE VARCHAR2(50 BYTE),
F_SIZE NUMBER(10),
BLOB_DATA BLOB DEFAULT empty_blob() );

DECLARE
dest blob; tmp_id varchar2(4);
v_f_sz NUMBER(10); b_file BFILE := NULL;
fName VARCHAR2 (50);

CURSOR get_items IS SELECT filename FROM My_Table_of_names;
BEGIN
FOR a_rec IN get_items LOOP
fName := a_rec.filename;
b_file := BFILENAME ( 'IMPORT_DIR', fName );
IF DBMS_LOB.fileexists (b_file) = 0 THEN
-- Report the problem and exit.
END IF;

DBMS_LOB.fileopen ( b_file, DBMS_LOB.file_readonly );
v_f_sz := DBMS_LOB.getlength (b_file);

INSERT INTO test_blobs ( ID, content_type, f_name, f_size, blob_data )
VALUES ( <Some_ID_val>, 'application/pdf', fName, v_f_sz, empty_blob() )
RETURNING blob_data, ID INTO dest, tmp_id;

-- Table field contains data, (HUGEBLOB),
-- when debugging and loop ends here.

DBMS_LOB.loadfromfile(dest, b_file, v_f_sz);
DBMS_LOB.close(b_file);

-- After running this, the variable tmp_id has the value that was assigned in the INSERT statement,
-- and the table’s fields, 'BLOB_Data', have the contents of the files. dest is indeed a reference
-- to the row and field, allowing LoadFromFile() to put data into the table.
END LOOP;
END;
/

关于oracle - DBMS_LOB.LoadFromFile 完整性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50805798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com