【发布时间】:2018-08-02 07:33:03
【问题描述】:
这是我的代码:
def insertDataFrameInDB(cursor, dataFrame, toTable, fieldNames = None):
if fieldNames:
dataFrame = dataFrame[fieldNames]
else:
fieldNames = dataFrame.columns
for r in dataFrame.columns.values:
dataFrame[r] = dataFrame[r].map(str)
dataFrame[r] = dataFrame[r].map(str.strip)
params = [tuple(x) for x in dataFrame.values]
fieldNameStr = ",".join(fieldNames)
valueStr = ",".join(["?"] * len(fieldNames))
sql = "INSERT INTO {} ({}) VALUES({})".format(toTable, fieldNameStr, valueStr)
cursor.fast_executemany = True
cursor.executemany(sql, params)
cursor.commit()
insertDataFrameInDB(cursor, df, "table")
它给出了以下我真的无法解决的错误:
DataError Traceback (most recent call last)
DataError: ('String data, right truncation: length 24 buffer 20', '22001')
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\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
SystemError: <built-in function utf_16_le_decode> returned a result with an error set
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
SystemError: decoding with 'utf-16le' codec failed (SystemError: <built-in function utf_16_le_decode> returned a result with an error set)
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\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
SystemError: <built-in function utf_16_le_decode> returned a result with an error set
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
SystemError: decoding with 'utf-16le' codec failed (SystemError: <built-in function utf_16_le_decode> returned a result with an error set)
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
<ipython-input-6-f73d9346f943> in <module>()
12
13 cursor = getCursor(conData)
---> 14 insertDataFrameInDB(cursor, df, "snowplow.sankey")
<ipython-input-1-69ecbca20fc8> in insertDataFrameInDB(cursor, dataFrame, toTable, fieldNames)
29 sql = "INSERT INTO {} ({}) VALUES({})".format(toTable, fieldNameStr, valueStr)
30 cursor.fast_executemany = True
---> 31 cursor.executemany(sql, params)
32 cursor.commit()
SystemError: <class 'pyodbc.Error'> returned a result with an error set
很多错误搜索让我觉得这与缺少 BOM 有关,我尝试解码“params”元组中的字符串,还尝试了 str.astype('U')。有谁知道导致问题的原因以及可能的解决方法?
【问题讨论】:
-
如果您使用的是 pyodbc 4.0.22,请尝试降级到 4.0.21 (
pip install pyodbc==4.0.21) 看看是否有帮助。 -
感谢您的输入,但遗憾的是错误仍然存在
-
如果您使用
cursor.fast_executemany = False...,错误是否仍然存在?另外,您使用的是什么 ODBC 驱动程序? -
通过设置 cursor.fast_executemany = False 解决,谢谢!但是我有很多数据,我用 True 读取它的执行速度大约快 20 倍。没有办法仍然使用 fast_executemany 吗?
-
fast_executemany可能与所有 ODBC 驱动程序不兼容。您使用的是哪个驱动程序?
标签: pandas sqlalchemy pyodbc