Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python and cx_Oracle - dynamic cursor.setinputsizes

I'm using cx_Oracle to select rows from one database and then insert those rows to a table in another database. The 2nd table's columns match the first select. So I have (simplified):

db1_cursor.execute('select col1, col2 from tab1')
rows = db1_cursor.fetchall()
db2_cursor.bindarraysize = len(rows)
db2_cursor.setinputsizes(cx_Oracle.NUMBER, cx_Oracle.BINARY)
db2_cursor.executemany('insert into tab2 values (:1, :2)', rows)

This works fine, but my question is how to avoid the hard coding in setinputsizes (I have many more columns). I can get the column types from db1_cursor.description, but I'm not sure how to feed those into setinputsizes. i.e. how can I pass a list to setinputsizes instead of arguments? Hope this makes sense - new to python and cx_Oracle

like image 478
Joe Watkins Avatar asked Jan 16 '12 15:01

Joe Watkins


1 Answers

Just use tuple unpacking. eg.

db_types = (d[1] for d in db1_cursor.description)
db2_cursor.setinputsizes(*db_types)
like image 76
Gerrat Avatar answered Nov 15 '22 11:11

Gerrat