Oracle照片批量导出:快捷实用方法(oracle 照片导出)
有时候,使用Oracle数据库存储的图像需要从Oracle表中批量导出 ,但Oracle的sqlplus客户端没有内置的图像导出功能。不过,可以使用以下PL / SQL脚本快速导出Oracle表中图像字段的内容:
SET SERVEROUTPUT ON
DECLARE
CURSOR cursor_name IS
SELECT * FROM photos;
img blob;
BEGIN
FOR Record IN cursor_name
LOOP
img := Record.image_blob;
IF img IS NOT NULL THEN
dbms_lob.FILEOPEN(1, ‘PATH\imagename.jpg’, dbms_lob.FILE_READWRITE);
— Writes BLOB upload_file to destination file
dbms_lob.WRITEAPPEND(1,length(img),img);
— Close File
dbms_lob.FILECLOSE(1);
END IF;
END LOOP;
END;
有两种方法可以使上述脚本工作:
首先,如果需要从Oracle库导出图像,可以编写一个简单的shell脚本(例如bash脚本)来使用SQL * PLUS执行上述PL / SQL脚本并将其输出保存到所需的目录。
第二,如果您正在使用Java或其它程序语言,则可以编写相关代码来读取Oracle数据库中的图像,然后将它们写入磁盘,简化为一个步骤:
//Create a connection to Oracle Database
String URL = “jdbc:oracle:thin:@::”;
Connection conn = DriverManager.getConnection(URL, “username”, “password”);
// Create a Prepared Statement so that the image can be retrived from the database.
PreparedStatement pstmt = conn.prepareStatement(“SELECT image_blob FROM photos”);
// Execute the query and retrieve the image from the result set
ResultSet rset = pstmt.executeQuery();
rset.next();
Blob blob = rset.getBlob(“image_blob”);
//Create an output stream to a file so that you can write the image
FileOutputStream outp = new FileOutputStream(“PATH\\imagename.jpg”);
//Read the blob and write to output stream
InputStream in = blob.getBinaryStream();
byte[] buffer = new byte[4096];
int length = -1;
while ((length = in.read(buffer)) != -1) {
outp.write(buffer, 0, length);
}
in.close();
outp.close();
由此可见,Oracle照片可以通过以上方法快速批量导出。使用PL/ SQL脚本或者编写相关程序来实现,可以快速,容易地实现Oracle照片的批量导出。