python 和 cx_Oracle - 动态 cursor.setinputsizes

标签 python cx-oracle

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

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 的新手

最佳答案

只需使用 tuple unpacking . 例如。

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

关于python 和 cx_Oracle - 动态 cursor.setinputsizes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881992/

相关文章:

python - 是否有比 pd.to_datetime 更快的日期转换?

python - "FFmpeg was not found, spotdl can' t continue”,即使它是使用 sudo apt-get install spotdl 安装的 + 如果从终端运行它也可以工作

Python cx_oracle 通过位置绑定(bind)变量忽略数字

python - 试图理解 cx_Oracle 的 LOB 对象

python - 如何操作来自 cx_Oracle 的查询结果

python - 如何在散点图中圈出不同的数据集?

python - 包装 pyspark Pipeline.__init__ 和装饰器

python - 在 Tensorflow GPU 中训练比 CPU 慢的简单模型

python - 将 Python 字典用于 CX_ORACLE 的 SQL INSERT 语句

Windows 7 上的 Python 3.5.3 和 Oracle 11.2 : cannot locate oracle include files