This is my code:
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")
It gives the following error which I really can't address:
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
A lot of error searching makes me think it has something to do with the lack of a BOM, I tried to decode the strings in the "params" tuples, also tried str.astype('U'). Does anybody know what causes the problem and possibly how to address that?