Please first make sure you have looked at:
Environment
To diagnose, we usually need to know the following, including version numbers. On Windows, be
sure to specify 32-bit Python or 64-bit:
- Python: 3.8.5 (OS X) and 3.8.7 (Windows 64 bit)
- pyodbc: 4.0.30
- OS: 10.15.7 (OS X) and 10 (Windows...unsure what build #)
- DB: SQL Server 2019
- driver: FreeTDS (OS X), ODBC Driver 17 for SQL Server (Windows)
Issue
When fast_executemany=True, long binary or string input values are not converted to the correct ODBC type it seems. Note that my examples below focus on using
Observed Behavior and MRE
Consider tables defined as follows:
CREATE TABLE binTest (
ID int,
bin varbinary
);
CREATE TABLE strTest (
ID int,
val varchar(max)
);
When running the following Python script (OS X example shown, but same occurs with equivalent Windows conn string), a
'String data, right truncation: length 8500 buffer 255'
error is returned for binTest.
import pyodbc
conn: pyodbc.Connection = pyodbc.connect("DSN=dockersql;UID=SA;PWD=?")
cur: pyodbc.Cursor = conn.cursor()
cur.fast_executemany = True
strToTest = 'a'*8500
binToTest = bytearray(strToTest,'utf8')
sql = """
INSERT INTO binTest VALUES (?,?)
"""
params = [(1, binToTest)]
cur.executemany(sql, params)
cur.commit()
sql = """
INSERT INTO strTest VALUES (?,?)
"""
params = [(1, strToTest)]
cur.executemany(sql, params)
cur.commit()
When commenting the binTest portion and doing the strTest portion, similar behavior happens:
'String data, right truncation: length 17000 buffer 510'
A workaround I have found is to manually specify the input type for the column as varbinary,0 or varchar,0
cur.setinputsizes([None, None, (pyodbc.SQL_VARBINARY,0)])
cur.setinputsizes([None, None, (pyodbc.SQL_VARCHAR,0)])
Please first make sure you have looked at:
Environment
To diagnose, we usually need to know the following, including version numbers. On Windows, be
sure to specify 32-bit Python or 64-bit:
Issue
When fast_executemany=True, long binary or string input values are not converted to the correct ODBC type it seems. Note that my examples below focus on using
Observed Behavior and MRE
Consider tables defined as follows:
When running the following Python script (OS X example shown, but same occurs with equivalent Windows conn string), a
error is returned for binTest.
When commenting the binTest portion and doing the strTest portion, similar behavior happens:
A workaround I have found is to manually specify the input type for the column as varbinary,0 or varchar,0
cur.setinputsizes([None, None, (pyodbc.SQL_VARBINARY,0)])cur.setinputsizes([None, None, (pyodbc.SQL_VARCHAR,0)])