写入到blob字段的存储过程

create or replace procedure write2blob(directions blob,
                                      v_blob     blob,
                                      v_pos      integer
                                      
                                      )is

  v_1time_maxlength integer; --一次读取的最大长度
  amount            integer; --总长度
  v_blob_sub        blob; --一次读取的长度
  v_index           integer; ---索引
  v_sub_count       integer;
  offset            integer;
  directions2       blob;
begin

  v_1time_maxlength := 32767; --允许的长度
  amount            := length(v_blob); --blob的总长度
  v_sub_count       := amount / v_1time_maxlength + 1; --分成子串的个数
  v_index           := 1; --索引从1开始
  offset            := v_pos;
  directions2       := directions;
  loop
    dbms_output.put_line(length(v_blob));  
  
    DBMS_LOB.read(v_blob, v_1time_maxlength, offset, v_blob_sub); --读至v_blob_sub中
    dbms_lob.writeappend(directions2, length(v_blob_sub), v_blob_sub); --将v_blob_sub中的内容写到字段中
    v_index := v_index + 1;
  
    offset := v_index * v_1time_maxlength + 1;
    exit when v_index > v_sub_count;
  
  end loop;

  dbms_lob.close(directions2);
exception
  when no_data_found then
    dbms_output.put_line('找不到数据');
end;

测试代码:

-- Created on 2013-3-8 by ZHANGXL 
declare 
  -- Local variables here
  i integer;
  v_blob blob;
  directions blob;
  v_post_id number(18):=8814;
begin
  select pt.post_text into v_blob from jforum_posts_text pt where pt.post_id=8817;
  -- Test statements here
  --execute updateblob(jforum_posts_text,post_id,post_text,8817,0,v_blob);
   --update jforum_posts_text pt
    --                       set pt.post_text = EMPTY_BLOB()
    --                     where pt.post_id = v_post_id;
  select pt.post_text into directions from jforum_posts_text pt where  pt.post_id=v_post_id for update; --锁住
  dbms_lob.open(directions, dbms_lob.lob_readwrite);--打开读写流
    write2blob(directions,v_blob,1);
  commit;
end;



表结构:

-- Create table
create table JFORUM_POSTS_TEXT
(
  POST_ID      NUMBER(18) not null,
  POST_TEXT    BLOB,
  POST_SUBJECT VARCHAR2(200),
  ISDELETED    NUMBER(1) default 0
)
tablespace XZFY
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 128
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table JFORUM_POSTS_TEXT
  add primary key (POST_ID)
  using index 
  tablespace XZFY
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

以上的存储过程都是自己写出来的,以下是根据不同的方法进行改造的,最要的问题在于一个超过32766长度的字符串,如果进行循环写入。改进如下:

CREATE OR REPLACE PROCEDURE INVHL_UPDATE_BLOB_DATA(p_table_name      IN VARCHAR2,--表名
                                 p_blob_field_name IN VARCHAR2,--blob列名
                                 p_where_condition IN VARCHAR2,--主键
                                 p_id              IN VARCHAR2,--id
                                 p_position        IN NUMBER,--开始位置,偏移量
                                 p_blob_data       IN blob)--数据
/*表名table_name,
   clob字段名field_name
   确定要更新唯一记录的where条件p_where_condition
  开始处理字符的位置p_position,
  传入的字符串变量P_clob_data*/
 IS
  v_lobloc       BLOB;
  v_blob_data    blob;
  v_amount       BINARY_INTEGER;
  v_position     BINARY_INTEGER;
  v_query_string VARCHAR2(1000);
  v_sub_length   binary_integer;
  v_sub_blob     blob;
  offset         binary_integer;
BEGIN
  v_position     := p_position * 32766 + 1;
  v_amount       := LENGTH(p_blob_data);
  v_blob_data    := p_blob_data;
  v_sub_length   := 32766;
  offset         :=1;
  v_query_string := 'SELECT ' || p_blob_field_name || ' FROM ' ||
                    p_table_name || ' WHERE ' || p_where_condition ||'='||p_id||
                    ' FOR UPDATE';
  dbms_output.put_line(v_query_string);
  --initialize buffer with data to be inserted or updated
  EXECUTE IMMEDIATE v_query_string
    INTO v_lobloc;

  --from pos position, write 32766 varchar2 into lobloc

    /** loop
        dbms_output.put_line(length(v_lobloc));
        dbms_output.put_line('v_amount='||v_amount||'v_sub_length='||v_sub_length||'offset='||offset);
        DBMS_LOB.read(v_lobloc, v_sub_length, offset, v_sub_blob);
        dbms_output.put_line('v_amount='||v_amount||'v_sub_length='||v_sub_length||'offset='||offset);
        DBMS_LOB.writeappend(v_lobloc, v_amount, v_sub_blob);
        offset:=offset+length(v_sub_blob);
        
         exit when offset >= v_amount;
                 
     end loop;**/

  DBMS_LOB.writeappend(v_lobloc, v_amount, v_blob_data);
  select lengthb(post_text) into v_amount from jforum_posts_text pt where post_id = p_id;
  dbms_output.put_line('v_amount'||v_amount);
  commit;
  
EXCEPTION
  WHEN OTHERS THEN
    ROLLBACK;
END;

测试代码:
-- Created on 2013-3-8 by ZHANGXL 
declare 
  -- Local variables here
  i integer;
  v_blob blob;
  directions blob;
  v_post_id number(18):=8814;
  offset integer :=1;
  v_sub_length integer :=32766;
  v_sub_blob blob;
begin
  select pt.post_text,lengthb(pt.post_text) into v_blob,i from jforum_posts_text pt where pt.post_id=8817;
  dbms_output.put_line('i='||i);
     
  
  -- Test statements here
  --execute updateblob(jforum_posts_text,post_id,post_text,8817,0,v_blob);
   /**update jforum_posts_text pt
                           set pt.post_text = EMPTY_BLOB()
                         where pt.post_id = v_post_id;
  select pt.post_text into directions from jforum_posts_text pt where  pt.post_id=v_post_id for update; --锁住
  dbms_lob.open(directions, dbms_lob.lob_readwrite);--打开读写流
    write2blob(directions,v_blob,1);
  commit;**/
  update jforum_posts_text pt
                           set pt.post_text = EMPTY_BLOB()
                         where pt.post_id = v_post_id;
                         
    if i > 32766 then 
       while offset < i loop
           DBMS_LOB.read(v_blob, v_sub_length, offset,v_sub_blob);
            invhl_update_blob_data('jforum_posts_text','post_text','post_id',v_post_id,1,v_sub_blob);
            offset:=length(v_sub_blob)+offset;
       end loop;
       else
            invhl_update_blob_data('jforum_posts_text','post_text','post_id',v_post_id,1,v_blob);
    end if;
 
  commit;
end;

可以继续改进的,需要继续研究。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 JDBC 向 Oracle 数据写入 BLOB 类型数据的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class WriteBlobToOracle { public static void main(String[] args) { // Oracle JDBC 驱动 String driver = "oracle.jdbc.driver.OracleDriver"; // 数据库连接信息 String url = "jdbc:oracle:thin:@localhost:1521:ORCL"; String user = "username"; String password = "password"; Connection conn = null; PreparedStatement pstmt = null; try { // 加载驱动程序 Class.forName(driver); // 获取数据库连接 conn = DriverManager.getConnection(url, user, password); // SQL 插入语句,参数使用 ? 占位符 String sql = "INSERT INTO blob_table (id, file_name, file_content) VALUES (?, ?, ?)"; // 创建 PreparedStatement 对象 pstmt = conn.prepareStatement(sql); // 设置参数 pstmt.setInt(1, 1); pstmt.setString(2, "test.txt"); // 读取文件内容到 InputStream 对象 File file = new File("test.txt"); InputStream is = new FileInputStream(file); // 设置 BLOB 类型参数 pstmt.setBinaryStream(3, is, (int) file.length()); // 执行 SQL 语句 int rowsAffected = pstmt.executeUpdate(); System.out.println(rowsAffected + " rows affected."); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭 PreparedStatement 和数据库连接 try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上面的示例中,我们使用 `setBinaryStream()` 方法将文件内容写入 BLOB 字段。该方法需要三个参数: - 参数索引:指定 BLOB 参数的位置,从 1 开始计数。 - InputStream 对象:包含要写入 BLOB 字段数据。 - BLOB 字段的长度:指定数据长度,使用文件的长度。 在插入数据之前,我们需要创建一个表来存储 BLOB 类型数据: ```sql CREATE TABLE blob_table ( id NUMBER(10) PRIMARY KEY, file_name VARCHAR2(50), file_content BLOB ); ``` 这个表包含三个字段: - `id`:表示唯一标识符。 - `file_name`:表示文件名。 - `file_content`:表示 BLOB 类型字段,用于存储文件内容。 最后,需要将文件 `test.txt` 放在项目根目录下,这个文件将被写入 BLOB 字段。如果需要写入其他文件,只需将文件名和路径更改为相应的值即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值