oracle存取二进制文件(图片)
时间: 2023-07-05 22:03:32 浏览: 160
Oracle数据库可以存储二进制文件(例如图片),下面是一个存储和检索示例:
1. 创建一个表来存储二进制文件:
```
CREATE TABLE image_table (
id NUMBER PRIMARY KEY,
image BLOB
);
```
2. 在该表中插入二进制文件,例如图片:
```
INSERT INTO image_table (id, image)
VALUES (1, EMPTY_BLOB());
COMMIT;
DECLARE
l_blob BLOB;
BEGIN
SELECT image INTO l_blob FROM image_table WHERE id = 1 FOR UPDATE;
DBMS_LOB.OPEN(l_blob, DBMS_LOB.LOB_READWRITE);
DBMS_LOB.LOADFROMFILE(l_blob, '/path/to/image.jpg');
DBMS_LOB.CLOSE(l_blob);
END;
/
```
3. 检索二进制文件:
```
SELECT image FROM image_table WHERE id = 1;
DECLARE
l_blob BLOB;
l_dest_offset INTEGER := 1;
l_src_offset INTEGER := 1;
l_lang_context INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
l_warning INTEGER;
BEGIN
SELECT image INTO l_blob FROM image_table WHERE id = 1;
DBMS_LOB.OPEN(l_blob, DBMS_LOB.LOB_READONLY);
DBMS_LOB.FILEOPEN(l_blob, DBMS_LOB.FILE_READONLY);
DBMS_LOB.LOADCLOBFROMFILE(l_blob, '/path/to/output.jpg', DBMS_LOB.GETLENGTH(l_blob), l_dest_offset, l_src_offset, l_lang_context, l_warning);
DBMS_LOB.FILECLOSE(l_blob);
DBMS_LOB.CLOSE(l_blob);
END;
/
```
上述代码将从数据库中检索图像并将其保存到本地文件系统中。
阅读全文