Ruby通过OCI8操作Oracle存储BLOB
2016-04-26 00:00
567 查看
摘要: 本文是一个Ruby读写Oracle的BLOB的例子,通过OCI8操作Oracle的存储过程
Oracle中BLOB是用来存储图片、文件等大数据对象的。
本文是一个Ruby读写Oracle的BLOB的例子,通过OCI8操作Oracle的存储过程
1、建表
2、写blob的存储过程
3、读blob的存储过程
4、ruby写图片操作
5、ruby读图片操作
参考教程
http://www.oracle.com/technetwork/cn/tutorials/rubyrails-095981-zhs.html
http://ruby-oci8.rubyforge.org/en/FAQ.html
Oracle中BLOB是用来存储图片、文件等大数据对象的。
本文是一个Ruby读写Oracle的BLOB的例子,通过OCI8操作Oracle的存储过程
1、建表
CREATE TABLE "T_IMAGE" ( "ID" NUMBER(11) GENERATED BY DEFAULT AS IDENTITY NOT NULL , "IMG" BLOB NULL )
2、写blob的存储过程
create or replace PROCEDURE P_WRITE_IMAGE ( I_PID IN NUMBER , B_IMG IN BLOB ) AS blob_temp BLOB; copy_amount integer; BEGIN insert into T_IMAGE values(I_PID,empty_blob()); commit; select IMG into blob_temp FROM T_IMAGE where id = I_PID FOR UPDATE; copy_amount := dbms_lob.getlength(B_IMG); dbms_lob.copy(blob_temp, B_IMG, copy_amount); commit; END P_WRITE_IMAGE;
3、读blob的存储过程
CREATE OR REPLACE PROCEDURE "P_READ_IMAGE" ( V_IMG_ID IN NUMBER, CUR_RESULT OUT SYS_REFCURSOR ) AS BEGIN OPEN CUR_RESULT FOR SELECT ID,IMG FROM T_IMAGE WHERE ID=V_IMG_ID ; END;
4、ruby写图片操作
require 'oci8' h_conn = OCI8.new(DB_USER, DB_PASSWORD, DB_SERVER) s_photo_full_path = "~/111.jpg" begin cursor = h_conn.parse('begin P_WRITE_IMAGE ( :photo_id, :photo ); end;') lob_photo = OCI8::BLOB.new(h_conn, File.read(s_photo_full_path, :mode => 'rb')) cursor.bind_param(':photo_id', nil, Fixnum) cursor.bind_param(':photo', lob_photo ) cursor.exec() rescue OCIError puts '-'*80 puts "Code: " + $!.code.to_s puts "Desc: " + $!.message puts '-'*80 end
5、ruby读图片操作
require 'oci8' h_conn = OCI8.new(DB_USER, DB_PASSWORD, DB_SERVER) s_photo_target_path = "~/222.jpg" photo_id = 1 begin cursor = h_conn.parse('begin P_READ_IMAGE( :img_id, :list ); end;') cursor.bind_param(':img_id', photo_id) cursor.bind_param(':list', nil, OCI8::Cursor) cursor.exec() ret_cursor = cursor[':list'] puts ret_cursor.getColNames.join(",") while row = ret_cursor.fetch() puts row[0] File.open(s_photo_target_path, 'wb') do |f| f.write(row[1].read) end break; end rescue OCIError puts '-'*80 puts "Code: " + $!.code.to_s puts "Desc: " + $!.message puts '-'*80 end
参考教程
http://www.oracle.com/technetwork/cn/tutorials/rubyrails-095981-zhs.html
http://ruby-oci8.rubyforge.org/en/FAQ.html
相关文章推荐
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- Oracle Containers for J2EE远程安全漏洞(CVE-2014-0413)
- Oracle 10g R2不能使用EM的问题
- 表空间操作
- PreparedStatement中in子句的处理
- VMware下RedHat4.8_64位安装Oracle 10g RAC--简略脚本
- oracle sql日期比较
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- OS block size和Oracle block size,查找OS Blocksize的方法
- 红宝石(Ruby)史话
- oracle中创建数据库和表空间的几点总结
- 数据库自动备份脚本
- Ruby简明教程之循环语句介绍
- ruby 一些简单的例子
- Ruby on Rails在Ping ++ 平台实现支付
- Ruby 之 class 中的 private、 protected、public
- Ruby中操作文件的方法介绍
- Ruby中的集合编写指南