Closed
Description
The Cursor.fast_executemany
feature introduced in 4.0.18/19 does not seem to handle Unicode characters correctly under Python3 when working with a SQL Server temporary table. The test code
import pyodbc
import sys
print('Python version ' + sys.version)
print('pyodbc version ' + pyodbc.version)
conn_str = (
r'DRIVER=ODBC Driver 11 for SQL Server;'
r'SERVER=(local)\SQLEXPRESS;'
r'DATABASE=myDb;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str, autocommit=True)
crsr = cnxn.cursor()
crsr.fast_executemany = False
crsr.execute("CREATE TABLE #tmp (id INT IDENTITY PRIMARY KEY, txt NVARCHAR(1))")
params = [('Ώ',), ('π',), ('α',)]
crsr.executemany("INSERT INTO #tmp (txt) VALUES (?)", params)
print(crsr.execute("SELECT txt FROM #tmp ORDER BY id").fetchall())
works as expected, producing
C:\Users\gord\PycharmVirtualenvs\py3pyodbc\Scripts\python.exe C:/Users/gord/PycharmProjects/py3pyodbc_test/main.py
Python version 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]
pyodbc version 4.0.19
[('Ώ', ), ('π', ), ('α', )]
Process finished with exit code 0
but when I enable the new feature with crsr.fast_executemany = True
I get
C:\Users\gord\PycharmVirtualenvs\py3pyodbc\Scripts\python.exe C:/Users/gord/PycharmProjects/py3pyodbc_test/main.py
Python version 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]
pyodbc version 4.0.19
[('?', ), ('p', ), ('a', )]
Process finished with exit code 0
SQL Profiler shows the following
exec sp_describe_undeclared_parameters N'INSERT INTO #tmp (txt) VALUES (@P1)'
exec sp_prepare @p1 output,N'@P1 varchar(255)',N'INSERT INTO #tmp (txt) VALUES (@P1)',1
exec sp_execute 1,'?'
exec sp_execute 1,'p'
exec sp_execute 1,'a'
Notice that the parameter type is mis-identified as varchar(255)
when it really should be nvarchar(1)
.