Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 359
Leo Avatar asked Mar 18 '23 20:03

Leo


2 Answers

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()
like image 166
Leo Avatar answered Apr 02 '23 09:04

Leo


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()
like image 39
Gareth Boyes Avatar answered Apr 02 '23 09:04

Gareth Boyes