一般都是通过程序往数据库存blob或clob类型的图片,偶尔需要临时直接插入一张图片到数据表。
1、创建测试表
CREATE TABLE IMAGE (T_ID VARCHAR2(5) NOT NULL, T_IMAGE BLOB NOT NULL);
2、创建图片指定目录(以Windows为例)
CREATE OR REPLACE DIRECTORY IMAGES AS 'E:pic';
3、创建存储过程
01.CREATE OR REPLACE PROCEDURE IMG_INSERT (TID VARCHAR2,FILENAME VARCHAR2) AS 02. F_LOB BFILE; 03. B_LOB BLOB; 04. BEGIN 05. INSERT INTO IMAGE (T_ID, T_IMAGE) 06. VALUES (TID,EMPTY_BLOB()) RETURN T_IMAGE INTO B_LOB; 07. F_LOB:= BFILENAME ('IMAGES', FILENAME); 08. DBMS_LOB.FILEOPEN (F_LOB, DBMS_LOB.FILE_READONLY); 09. DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB, 10. DBMS_LOB.GETLENGTH (F_LOB)); 11. DBMS_LOB.FILECLOSE (F_LOB); 12. COMMIT; 13. END;
4、在E:pic目录下放入一张测试图片,执行存储过程
CALL IMG_INSERT('1','test.jpg');