python和cx_Oracle-动态cursor.setinputsizes [英] python and cx_Oracle - dynamic cursor.setinputsizes

查看:224
本文介绍了python和cx_Oracle-动态cursor.setinputsizes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cx_Oracle从一个数据库中选择行,然后将这些行插入到另一个数据库中的表中。第二张表格的列与第一个表格匹配。
所以(简化):

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)

这很好,但我的问题是如何避免在setinputsizes中进行硬编码(我还有更多列)。
我可以从db1_cursor.description中获取列类型,但是我不确定如何将它们输入setinputsizes中。即如何将列表传递给setinputsizes而不是参数?
希望这有意义-python和cx_Oracle的新手

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

推荐答案

只需使用元组拆包
例如。

Just use tuple unpacking. eg.

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

这篇关于python和cx_Oracle-动态cursor.setinputsizes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆