首页 > 解决方案 > Python3 ODBC 执行许多

问题描述

我正在尝试使用 python odbc 库将数据从 1 个 oracle 表复制到另一个模式中。这就是我正在做的

source = SomeString (source Oracle DataTable) target = SomeString (target Oracle DataTable)

连接到数据源以检索数据: source_data = pyodbc.connect(source) source_cursor = source_data.cursor()

连接到目标数据源 target = pyodbc(target) target_cursor = target.cursor()

我现在声明我的源数据查询 source_query = SELECT * FROM TABLE where TYPE = X

我将数据放入数据框中,然后将其转换为列表 data = pd.read_sql(source_query, source) data = data.values.tolist()

我现在正试图将我的“数据”列表中的数据插入到我的目标表中。我声明了一个插入语句,然后按如下方式运行 executemany:

sql = "INSERT INTO SCHEMA.TABLE (column1, column 2, etc...) Values (?,?, etc..)

现在,由于我建立了数据和目标连接,因此我执行以下操作 target_cursor.executemany(sql, data)

我在下面收到以下错误,奇怪的是代码在新表中正确插入了 1 行,然后它失败了,没有任何反应。

你能指导我如何解决这个问题吗?

我收到以下错误:

C:\WinPy3770x64\python-3.7.7.amd64\lib\encodings\utf_16_le.py in decode(input, errors)
     15 def decode(input, errors='strict'):
---> 16     return codecs.utf_16_le_decode(input, errors, True)
     17 

UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 184-185: illegal encoding

The above exception was the direct cause of the following exception:
SystemError                               Traceback (most recent call last)
<ipython-input-70-ec2d225d6132> in <module>
----> 1 target_cursor.executemany(sql_statement, data)

SystemError: <class 'pyodbc.Error'> returned a result with an error set

标签: pythonpandasoraclepyodbcexecutemany

解决方案


推荐阅读