python blob operation

Recently in learning to use Python, Oracle database using operating cx_Oracle module.

For the basic fields, can be normal operation. But for Blob fields, I try several times, without success. Test code posted below, and we discuss the discussion.

This is a persistent object operation. I have just touched on Python, we do not know there is no good Python ORM framework.


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



Wherein, report table "HTMLTEMPLATE" field is blob types.

Here is the test program. The main purpose of the program is to complete data migration.



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()



The above procedure, if the BLOB field is empty, it can be performed by, if not empty, it can not be inserted.


In this communication to share with you the next.

Guess you like

Origin www.cnblogs.com/ruiy/p/11753413.html