I have text file containing a list of 16 bit hex numbers (e.g. '61C7393AA9B3474DB081C7B7CCE1C545') and I need to insert them an Oracle RAW column using cx_Oracle. I tried this:

sql = "INSERT INTO GUIDS VALUES (HEXTORAW(:1))"
ids = [l.strip() for l in guidfile.readlines()]
cursor.bindarraysize = len(ids)
cursor.setinputsizes(cx_Oracle.BINARY)
cursor.executemany(sql, ids)

but it fails with cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number. Do I need to convert the values to binary in python before calling executemany ? And if so how? NB the same sql works fine with cursor.execute and a single value, it just with the list that I'm having problems.

有帮助吗?

解决方案

Well, I've solved it, in a couple of ways. Firstly, the code above is incorrect as it creates a list of values whereas it should be creating a list of tuples. i.e.

ids = [(l.strip(),) for l in guidfile.readlines()]

and then using

cursor.setinputsizes(cx_Oracle.STRING)

it works. On the original question - how to get rid of the hextoraw, I found that this works:

import base64
ids = [(base64.b16decode(l.strip()),) for l in guidfile.readlines()]
cursor.setinputsizes(cx_Oracle.BINARY)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top