Python如何操作Oracle的Blob字段

最近在学习使用Python,操作Oracle数据库采用的是cx_Oracle模块。

对于基本字段,都可以正常操作。但是对于Blob字段,我试试好几次,都没成功。下面贴出测试代码,与大家讨论讨论。

这是操作的持久对象。本人对Python刚刚接触,不知道Python有没有好的ORM框架。

class Report(object):
    
    selectSql="select RPTID,CLGID,RPTNAME,RPTTYPE,RPTDESC,QUALITYSIGNALS,DISPLAYSETTING,EXCELRANGE,HTMLTEMPLATE from rpt_report order by rptid"
    
    createTableSql='''
/*==============================================================*/
/* Table: RPT_REPORT                                            */
/*==============================================================*/
create table RPT_REPORT  (
   RPTID                NUMBER                          not null,
   CLGID                NUMBER,
   RPTNAME              VARCHAR2(50)                    not null,
   RPTTYPE              NUMBER                          not null,
   RPTDESC              VARCHAR2(100),
   QUALITYSIGNALS       VARCHAR2(50),
   DISPLAYSETTING       VARCHAR2(50),
   EXCELRANGE           VARCHAR2(30),
   HTMLTEMPLATE         BLOB,
   constraint PK_RPT_REPORT primary key (RPTID)
           using index
);
    '''
    
    deleteSql="delete from rpt_report"
    
    def __init__(self,RPTID,CLGID,RPTNAME,RPTTYPE,RPTDESC,QUALITYSIGNALS,DISPLAYSETTING,EXCELRANGE,HTMLTEMPLATE):
      
        self.RPTID=RPTID
        self.CLGID=CLGID
        self.RPTNAME=RPTNAME
        self.RPTTYPE=RPTTYPE
        self.RPTDESC=RPTDESC and RPTDESC or ""
        self.QUALITYSIGNALS=QUALITYSIGNALS and QUALITYSIGNALS or ""
        self.DISPLAYSETTING=DISPLAYSETTING and DISPLAYSETTING or ""
        self.EXCELRANGE=EXCELRANGE and EXCELRANGE or ""
        self.HTMLTEMPLATE=HTMLTEMPLATE and HTMLTEMPLATE or ""
    
    def gerInsertSQL(self):
        sql="insert into rpt_report(RPTID,CLGID,RPTNAME,RPTTYPE,RPTDESC,QUALITYSIGNALS,DISPLAYSETTING,EXCELRANGE,HTMLTEMPLATE) values (%d,%d,'%s',%d,'%s','%s','%s','%s',:HTMLTEMPLATE)" 
        return sql   


其中,report表的“HTMLTEMPLATE”字段是blob类型。

下面是测试程序。程序的目的主要是完成数据迁移。


import cx_Oracle
import sys
#1.5 database
connection = cx_Oracle.Connection("pw_report/report@tpri")
cursor = connection.cursor()
#2.0 database
conn=cx_Oracle.Connection("pw_report2/report@tpri")
cur=conn.cursor()

cursor.execute(Report.selectSql)

cursor.rowfactory = Report
index=0
isRollback=False



for row in cursor:
    sql=row.gerInsertSQL()
    try:
        HTMLTEMPLATE=row.HTMLTEMPLATE and row.HTMLTEMPLATE or ""
        
        sql=sql % (row.RPTID,row.CLGID,row.RPTNAME,row.RPTTYPE,row.RPTDESC,row.QUALITYSIGNALS,row.DISPLAYSETTING,row.EXCELRANGE)
        print sql
        cur.setinputsizes(HTMLTEMPLATE=cx_Oracle.BLOB)
        cur.execute(sql,{"HTMLTEMPLATE":HTMLTEMPLATE})
        
        index+=1
    except cx_Oracle.DatabaseError, exc:
        print "break le 11"
        isRollback=True
        break
    except:
        print "break le 22"
        isRollback=True
        break


print "\n insert data: %d" % index
if isRollback:
    conn.rollback()
else:
    conn.commit()
conn.close()
connection.close()



以上程序,若BLOB字段内容为空,就可以执行通过,若不为空,则无法插入。


在此与大家分享沟通下。

你可能感兴趣的:(oracle,sql,框架,python,orm)