python and cx_Oracle - dynamic cursor.setinputsizes

谁说胖子不能爱 提交于 2019-12-12 11:45:30

问题


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


回答1:


Just use tuple unpacking. eg.

db_types = (d[1] for d in db1_cursor.description)
db2_cursor.setinputsizes(*db_types)


来源:https://stackoverflow.com/questions/8881992/python-and-cx-oracle-dynamic-cursor-setinputsizes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!