简体   繁体   中英

cx_oracle how to update blob column

could anyone help with how to update blob data in oracle

so, i'm trying like:

file = open ('picture.jpg','rb') 
ext = 'jpg'
content = file.read ()
file.close ()
db = cx_Oracle.connect('user', 'pwd', dsn_tns)
db=db.cursor()
sqlStr = "update table_name set column1=:blobData, column2=" + str(ext) + " where id = 1"
db.setinputsizes (blobData = cx_Oracle.BLOB)
db.execute (sqlStr, {'blobData': content})
db.execute ('commit')
db.close()

finally, I got such error:

cx_Oracle.DatabaseError: ORA-00904: "JPG": invalid identifier
file = open ('picture.jpg','rb') 
ext = 'jpg'
content = file.read ()
file.close ()
db = cx_Oracle.connect('user', 'pwd', dsn_tns)
db=db.cursor()
blobvar = db.var(cx_Oracle.BLOB)
blobvar.setvalue(0,content)
sqlStr = "update table_name set column1=:blobData, column2="jpg" where id = 1"
db.setinputsizes (blobData = cx_Oracle.BLOB)
db.execute (sqlStr, {'blobData': blobvar})
db.execute ('commit')
db.close()

cx_Oracle 6.0 does not allow you to close a connection whilst a BLOB is open leading to a DPI-1054 error.

cx_Oracle.DatabaseError: DPI-1054: connection cannot be closed when open statements or LOBs exist

Adding to Leo's answer, this can be resolved by deleting the BLOB variable.

file = open ('picture.jpg','rb') 
ext = 'jpg'
content = file.read ()
file.close ()
con = cx_Oracle.connect('user', 'pwd', dsn_tns)
db = con.cursor()
blobvar = db.var(cx_Oracle.BLOB)
blobvar.setvalue(0,content)
sqlStr = "update table_name set column1=:blobData, column2="jpg" where id = 1"
db.setinputsizes (blobData = cx_Oracle.BLOB)
db.execute (sqlStr, {'blobData': blobvar})

del blobvar  #  <-------- 

db.execute ('commit')
db.close()
con.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM