Cx_oracle how to update blob column
can anyone help with how to update blob data in oracle
So, I am 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 an error like this:
cx_Oracle.DatabaseError: ORA-00904: "JPG": invalid identifier
+3
source to share
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()
+5
source to share
cx_Oracle 6.0 does not allow the connection to be closed while the BLOB is open, resulting in 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 solved 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()
+2
source to share