oraclepythonclob
『壹』 python如何保存從oracle資料庫中讀取的BLOB文件
import cx_Oracle
con = cx_Oracle.connect(『username』, 『password』, 『dsn』)
blob_sql = "select column_name from table where clause"
cursor = con.cursor()
cursor.execute(blob_sql)
result = cursor.fetchall()
file = open('file_name', "wb")
file.write(result[0][0].read()) #可以print查看result的內容,根據實際情況read
file.close()
『貳』 如何從ORACLE中讀取CLOB類型的數據
MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,並且改名為MyBatis 。2013年11月遷移到Github。
iBATIS一詞來源於「internet」和「abatis」的組合,是一個基於java的持久層框架。iBATIS提供的持久層框架包括SQL Maps和Data Access Objects(DAO)
2、CLOB
SQL CLOB 是內置類型,它將字元大對象 (Character Large Object) 存儲為資料庫表某一行中的一個列值。默認情況下,驅動程序使用 SQL locator(CLOB) 實現 Clob 對象,這意味著 CLOB 對象包含一個指向 SQL CLOB 數據的邏輯指針而不是數據本身。Clob 對象在它被創建的事務處理期間有效。
3、MyBatis對CLOB類型數據實現增刪改查
oracle表結構
1
2
3
4
5
6
7
8
9
10
11
12
13
14
create table T_USERS
(
ID NUMBER not null,
NAME VARCHAR2(30),
SEX VARCHAR2(3),
BIRS DATE,
MESSAGE CLOB
)
create sequence SEQ_T_USERS_ID
minvalue 1
maxvalue 99999999
start with 1
increment by 1
cache 20;
配置mybatis配置文件UsersMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN">
<mapper namespace="examples.mapper.UsersMapper" >
<!-- Result Map-->
<resultMap type="examples.bean.Users" id="BaseResultMap">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="birs" column="birs" jdbcType="TIMESTAMP"/>
<result property="message" column="message" jdbcType="CLOB"
javaType = "java.lang.String" typeHandler ="examples.service.OracleClobTypeHandler"/>
</resultMap>
<sql id="Tabel_Name">
t_users
</sql>
<!-- 表中所有列 -->
<sql id="Base_Column_List" >
id,name,sex,birs,message
</sql>
<!-- 查詢條件 -->
<sql id="Example_Where_Clause">
where 1=1
<trim suffixOverrides=",">
<if test="id != null">
and id = #{id}
</if>
<if test="name != null and name != ''">
and name like concat(concat('%', '${name}'), '%')
</if>
<if test="sex != null and sex != ''">
and sex like concat(concat('%', '${sex}'), '%')
</if>
<if test="birs != null">
and birs = #{birs}
</if>
<if test="message != null">
and message = #{message}
</if>
</trim>
</sql>
<!-- 2.查詢列表 -->
<select id="queryByList" resultMap="BaseResultMap" parameterType="Object">
select
<include refid="Base_Column_List" />
from t_users
<include refid="Example_Where_Clause"/>
</select>
</mapper>
Mapper類介面
1
2
3
4
5
6
7
8
9
10
11
package examples.mapper;
import java.util.List;
public interface UsersMapper<T> {
public List<T> queryBySelective(T t);
public List<T> queryByList(T t);
}
類型轉換工具類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package examples.service;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.CLOB;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
public class OracleClobTypeHandler implements TypeHandler<Object> {
public Object valueOf(String param) {
return null;
}
@Override
public Object getResult(ResultSet arg0, String arg1) throws SQLException {
CLOB clob = (CLOB) arg0.getClob(arg1);
return (clob == null || clob.length() == 0) ? null : clob.getSubString((long) 1, (int) clob.length());
}
@Override
public Object getResult(ResultSet arg0, int arg1) throws SQLException {
return null;
}
@Override
public Object getResult(CallableStatement arg0, int arg1) throws SQLException {
return null;
}
@Override
public void setParameter(PreparedStatement arg0, int arg1, Object arg2, JdbcType arg3) throws SQLException {
CLOB clob = CLOB.empty_lob();
clob.setString(1, (String) arg2);
arg0.setClob(arg1, clob);
}
}
Spring配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
xmlns:xsi="
xmlns:mvc="
xmlns:tx="
xsi:schemaLocation="
default-autowire="byType">
<!-- 配置數據源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@127.0.0.1:1521:pms</value></property>
<property name="username"><value>pms</value></property>
<property name="password"><value>pms</value></property>
</bean>
<!-- 配完數據源 和 擁有的 sql映射文件 sqlSessionFactory 也可以訪問資料庫 和擁有 sql操作能力了 -->
<!--
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath:examples/mybatis/oracle/UsersMapper.xml</value>
</list>
</property>
</bean>
<!-- 通過設置 mapperInterface屬性,使介面服務bean 和對應xml文件管理 可以使用其中的sql -->
<bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 此處等同於 Mybatis 中 ServerDao serverDao = sqlSession.getMapper(ServerDao.class); 指明映射關系 -->
<property name="mapperInterface" value="examples.mapper.UsersMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
測試類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package examples.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.;
import examples.bean.Users;
import examples.mapper.UsersMapper;
public class TestUsersService {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws ParseException {
ApplicationContext ac =
new ("classpath:/examples/service/spring.xml");
UsersMapper<Users> = (UsersMapper<Users>)ac.getBean("");
//查詢
Users nullBean = new Users();
List<Users> list = .queryByList(nullBean);
if(list != null) {
for(Users user : list) {
System.out.println(user);
}
}
}
}
『叄』 怎麼從ORACLE中讀取CLOB類型的數據
這樣讀取:declare /*聲明PL/SQL中程序塊中的變數info_var,用戶存放查詢到的info列的數據,其類型必須和表中的欄位類型一致*/ info_var clob; --查詢數據長度 amount integer; --偏移量,查詢起始位置 offset integer; --需要列印的位元組,存儲變數 info_output varchar2(1000); begin --查詢要列印的欄位信息並賦值給info_var select info into info_var from t_clob where id = 1; --查詢100長度 amount :=100; --從第一個開始 offset :=1; --用dbms_lob程序包讀取數據 dbms_lob.read(info_var,amount,offset,info_var); --用dbms_lob程序包列印讀取得數據info_var dbms_output.put_line(info_var); end; /
『肆』 oracle如何操作clob數據類型
在做資料庫開發的時候,有時候會遇到需要讀取Oracle資料庫中的clob類型的數據的情況。本著代碼復用的目的,寫了下面的存儲過程:讀取資料庫中clob欄位的數據。
CREATE OR REPLACE PROCEDURE prc_read_clob(
table_name IN VARCHAR2,
clob_column_name IN VARCHAR2,
primary_Key_Column_names IN VARCHAR2,
primary_key_values IN VARCHAR2,
offset_i IN NUMBER,
read_length_i IN NUMBER,
RES OUT VARCHAR2,
total_length OUT NUMBER
) AS
/**
Autor:Hanks_gao.
Create Date:2008/12/10
Description:This procere is to read clob value by conditions
--------------------------------------------------------------
-----------------Parameters descritption----------------------
table_name : The table that contains clob/blob columns(表名)
clob_column_name : Clob/blob column name of table_name(類型為clob的欄位名)
primary_key_column_names : The columns seperated by '}' that can fix only one row data (that is primary key) (主鍵名,以'}'分隔的字元串)
primary_key_values : The primary keyes values that seperated by '}'(主鍵鍵值,以'}'分隔的字元串)
offset_i : The offset of reading clob data(要讀取的位移量)
read_length_i : The length of reading clob data per times(要讀取的長度)
res : Return value that can be referenced by application(讀取的結果)
total_length : The total length of readed clob data(資料庫查詢到的clob數據的總長度)
-----------------End Parameters descritption------------------
*/
tmpPrimaryKeys VARCHAR2(2000); --To save primary_Key_Column_names temporarily(暫存主鍵,主鍵是以'}'分隔的字元串)
tmpPrimaryKeyValues VARCHAR2(2000); --To save primary_key_values temporarily(暫存主鍵鍵值,以'}'分隔的字元串)
i NUMBER; --循環控制變數
tmpReadLength NUMBER; --暫存要讀取的長度
sqlStr VARCHAR2(6000); --Query string(查詢字元串)
sqlCon VARCHAR2(5000); --Query condition(查詢條件)
TYPE tmparray IS TABLE OF VARCHAR2(5000) INDEX BY BINARY_INTEGER;
arrayPrimaryKeys tmparray; --To save the analyse result of primary_Key_Column_names (暫存分析後得到的主鍵名)
arrayPrimaryKeyValues tmparray; --To save the analyse result of primary_key_values(暫存分析後得到的主鍵鍵值)
BEGIN
total_length := 0;
RES := '';
DECLARE
clobvar CLOB := EMPTY_CLOB;
BEGIN
tmpPrimaryKeys:=primary_Key_Column_names;
tmpPrimaryKeyValues:=primary_key_values;
i:=0;
WHILE INSTR(tmpPrimaryKeys,'}')>0 LOOP --Analyse the column names of primary key(將主鍵分開,相當於arrayPrimaryKeys =tmpPrimaryKeys.split("}") )
arrayPrimaryKeys(i):=subSTR(tmpPrimaryKeys,1,(INSTR(tmpPrimaryKeys,'}')-1));
tmpPrimaryKeys:=subSTR(tmpPrimaryKeys,(INSTR(tmpPrimaryKeys,'}')+1));
i:=i+1;
END LOOP;
i:=0;
WHILE INSTR(tmpPrimaryKeyValues,'}')>0 LOOP --Analyse the values of primary key
arrayPrimaryKeyValues(i):=subSTR(tmpPrimaryKeyValues,1,(INSTR(tmpPrimaryKeyValues,'}')-1));
tmpPrimaryKeyValues:=subSTR(tmpPrimaryKeyValues,(INSTR(tmpPrimaryKeyValues,'}')+1));
i:=i+1;
END LOOP;
IF arrayPrimaryKeys.COUNT()<>arrayPrimaryKeyValues.COUNT() THEN --判斷鍵與鍵值是否能匹配起來
res:='KEY-VALUE NOT MATCH';
RETURN;
END IF;
i := 0;
sqlCon := '';
WHILE i < arrayPrimaryKeys.COUNT() LOOP
sqlCon := sqlCon || ' AND ' || arrayPrimaryKeys(i) || '='''
|| replace(arrayPrimaryKeyValues(i),'''','''''') || '''';
i := i + 1;
END LOOP;
sqlStr := 'SELECT ' || clob_column_name || ' FROM ' || table_name
|| ' WHERE 1=1 ' || sqlCon || ' AND ROWNUM = 1' ; --組查詢字元串
dbms_lob.createtemporary(clobvar, TRUE);
dbms_lob.OPEN(clobvar, dbms_lob.lob_readwrite);
EXECUTE IMMEDIATE TRIM(sqlStr) INTO clobvar; --執行查詢
IF offset_i <= 1 THEN
total_length:=dbms_lob.getlength(clobvar);
END IF;
IF read_length_i <=0 THEN
tmpReadLength := 4000;
ELSE
tmpReadLength := read_length_i;
END IF;
dbms_lob.READ(clobvar,tmpReadLength,offset_i,res); --讀取數據
IF dbms_lob.ISOPEN(clobvar)=1 THEN
dbms_lob.CLOSE(clobvar);
END IF;
END;
EXCEPTION
WHEN OTHERS THEN
res:='';
total_length:=0;
END;
『伍』 oracle資料庫中clob類型怎麼查詢博客園
在絕大多數情況下,使用2種方法使用CLOB
1 相對比較小的,可以用String進行直接操作,把CLOB看成字元串類型即可
2 如果比較大,可以用 getAsciiStream 或者 getUnicodeStream 以及對應的 setAsciiStream 和 setUnicodeStream 即可
讀取數據
1 ResultSet rs = stmt.executeQuery("SELECT TOP 1 * FROM Test1");
2 rs.next();
3 Reader reader = rs.getCharacterStream(2);
『陸』 Oracle中Blob和Clob的作用
BLOB是用來存儲大量二進制數據的;CLOB用來存儲大量文本數據。
『柒』 如何對ORACLE里的CLOB欄位進行模糊查詢
對oracle中clob進行模糊查詢需要將clob的內容轉成字元類型,然後才可以用模糊查詢。
舉例:
1、表中錄入以下數據:
createtabletest
(idint,
strclob);
insertintotestvalues(1,'東東是壞人');
insertintotestvalues(2,'物理是壞人');
insertintotestvalues(3,'小青蛙是壞人');
insertintotestvalues(4,'badkano是好人');
commit;
2、現在查詢test表中str欄位中含有「壞人」的內容,用如下語句:
select*fromtestwhereto_char(str)like'%壞人%';
3、查詢結果(其中查到三條記錄,但clob的內容無法直接顯示)
『捌』 怎麼用Python腳本怎麼從oracle資料庫中取出clob數據
stmt = con.prepareStatement("select attach,fjmc,piid,swsj fromreceiveFile");//attach是clolb對象
rs = stmt.executeQuery( );
while (rs.next()) {
java.sql.Blob blob = rs.getBlob(1);//這一句可獲得blob,clob等對象。
然後再把blob轉成文件
File file = new File("G:\\XiangMu_dwoa\\資料庫文件資料\\aaa");
OutputStream fout = new FileOutputStream(file);
//下面將BLOB數據寫入文件
byte[] b = new byte[1024];
int len = 0;
while ( (len = ins.read(b)) != -1) {
fout.write(b, 0, len);
你可以參考一下
『玖』 oracle 中clob內容怎麼查詢
to_char轉換一下就行了
select其他欄位,to_char(clob欄位)from表名