BLOB转换为CLOB的函数(oracle中执行)

BLOB转换为CLOB的函数(oracle中执行)

CREATE OR REPLACE FUNCTION BlobToClob(blob_in IN BLOB) RETURN CLOB AS

v_clob CLOB;

v_varchar VARCHAR2(32767);

v_start PLS_INTEGER := 1;

v_buffer PLS_INTEGER := 32767;

BEGIN

DBMS_LOB.CREATETEMPORARY(v_clob, TRUE);

FOR i IN 1 .. CEIL(DBMS_LOB.GETLENGTH(blob_in) / v_buffer) LOOP

v_varchar := UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(blob_in,

v_buffer,

v_start));

DBMS_LOB.WRITEAPPEND(v_clob, LENGTH(v_varchar), v_varchar);

DBMS_OUTPUT.PUT_LINE(v_varchar);

v_start := v_start + v_buffer;

END LOOP;

RETURN v_clob;

END BlobToClob;

一、 BLOBCLOB的区别

1. BLOB使用二进制保存数据,CLOB使用CHAR来保存数据。

2. CLOBNCLOBBLOB都是内部的LOB(Large Object)类型,最长4G,要保存图片、文本文件、Word文件最好使用BLOB类型。

二、 常用操作:

Get()方法:

CLOB

//获得数据库连接

Connection con = ConnectionFactory.getConnection();

con.setAutoCommit(false);

Statement stmt = con.createStatement();

//不需要“for update

ResultSet rs = stmt.executeQuery("select CLOBATTR from TESTCLOB where ID=1");

if (rs.next())

{

java.sql.Clob clob = rs.getClob("CLOBATTR");

Reader inStream = clob.getCharacterStream();

char[] c = new char[(int) clob.length()];

inStream.read(c);

//data是读出并需要返回的数据,类型是String

data = new String(c);

inStream.close();

}

inStream.close();

con.commit();

con.close();

BLOB

//获得数据库连接

Connection con = ConnectionFactory.getConnection();

con.setAutoCommit(false);

Statement st = con.createStatement();

//不需要“for update

ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");

if (rs.next())

{

java.sql.Blob blob = rs.getBlob("BLOBATTR");

InputStream inStream = blob.getBinaryStream();

//data是读出并需要返回的数据,类型是byte[]

data = new byte[input.available()];

inStream.read(data);

inStream.close();

}

inStream.close();

con.commit();

con.close();

你可能感兴趣的:(oracle,sql,C++,c,C#)