如何将映像加载到oracle数据库表中

时间:2012-11-29 09:57:31

标签: sql database oracle plsql

如何将图像加载到oracle数据库表

假设我有一张像图像一样的表

CREATE TABLE image (img_id number
                  ,img_name varchar2(100)
                  ,img   BLOB) ;

任何人都可以帮助我

2 个答案:

答案 0 :(得分:3)

CREATE OR REPLACE DIRECTORY test_dir AS 'C:\'; 

DECLARE
  l_bfile  BFILE;
  l_blob   BLOB;
BEGIN
  INSERT INTO tab1 (col1, col2)
  VALUES (empty_blob(), 'test1')
  RETURN col1 INTO l_blob;

  l_bfile := BFILENAME('test_dir', 'Test.txt');
  DBMS_LOB.fileopen(l_bfile, Dbms_Lob.File_Readonly);
  DBMS_LOB.loadfromfile(l_blob, l_bfile, DBMS_LOB.getlength(l_bfile));
  DBMS_LOB.fileclose(l_bfile);

  COMMIT;
END;

答案 1 :(得分:2)

如果文件位于数据库服务器上,请参阅罗伯特对该方法的回答。

如果文件位于CLIENT文件系统上,请使用SQLLDR。例如:

SQL> CREATE TABLE tab1 (
  2    id number primary key,
  3    image      BLOB
  4  );

Table created.

SQL> host sqlldr user/pass@yourdb control=load_image.ctl

SQL*Loader: Release 11.2.0.2.0 - Production on Thu Nov 29 10:41:08 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Commit point reached - logical record count 1

SQL> select * from tab1;

        ID
----------
IMAGE
--------------------------------------------------------------------------------
         1
FFD8FFE000104A46494600010101004800480000FFE111C44578696600004D4D002A00000008000B
010F00020000000100000000011000020000000100000000011200030000000100010000011A0005


SQL> select length(image) from tab1;

LENGTH(IMAGE)
-------------
       263508

SQL>

控制文件是这样的:

LOAD DATA 
INFILE 'images.txt'
INTO TABLE tab1
FIELDS TERMINATED BY ','
(id  ,
 image_filename    FILLER CHAR(100),
 image             LOBFILE(image_filename) TERMINATED BY EOF)

和示例images.txt文件:

1,foo.jpg