cx_oracle如何更新blob列

时间:2014-11-19 06:42:43

标签: python blob cx-oracle

任何人都可以帮助解决如何更新oracle中的blob数据

所以,我想尝试:

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

最后,我收到了这样的错误:

cx_Oracle.DatabaseError: ORA-00904: "JPG": invalid identifier

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

cx_Oracle 6.0不允许您在BLOB打开时关闭连接,从而导致DPI-1054错误。

  

cx_Oracle.DatabaseError:DPI-1054:当存在打开的语句或LOB时,无法关闭连接

添加到Leo的答案,可以通过删除BLOB变量来解决这个问题。

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