Skip to content

Long byte string / regular string not binding as varbinary(max), varchar(max) when fast_executemany=True #867

Description

@ww2406

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)])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions