cursor.columns doesn't return column names
See original GitHub issueEnvironment
- Python: 2.7.15 (32 Bit)
- pyodbc: 4.0.25 (verified that problem doesn’t exist on 4.0.24 with exactly the same code)
- OS: Windows 10
- DB: SQL Server 2017
- driver: {ODBC Driver 13 for SQL Server}, {SQL Server Native Client 11.0}, {SQL Server}
Issue
This issue came up with version 4.0.25 of pyodbc on Python 2.7.15, 32 bit on Windows 10 and was not present in version 4.0.24. The cursor.columns() call intermittently fails to return the columns in a table. To exercise the bug, when I create table names with the following lengths (and sometimes other lengths) as such:
from __future__ import print_function, unicode_literals
import pyodbc
cnxn = pyodbc.connect(r"DRIVER={ODBC Driver 13 for SQL Server};SERVER=srv;DATABASE=test_db;Trusted_Connection=yes;")
sql = """CREATE TABLE A234567
(
ID TINYINT,
PRIMARY KEY (ID)
);
"""
with cnxn:
cnxn.execute(sql)
sql = """CREATE TABLE A
(
ID TINYINT,
PRIMARY KEY (ID)
);
"""
with cnxn:
cnxn.execute(sql)
And then I try and get the list of column names for those tables as such:
from __future__ import print_function, unicode_literals
import pyodbc
cnxn = pyodbc.connect(r"DRIVER={ODBC Driver 13 for SQL Server};SERVER=srv;DATABASE=test_db;Trusted_Connection=yes;")
curs = cnxn.cursor()
print([col.column_name for col in curs.columns('A234567')])
print([col.column_name for col in curs.columns('A234567')])
# Fine from here for A234567
curs = cnxn.cursor()
print([col.column_name for col in curs.columns('A')])
print([col.column_name for col in curs.columns('A')])
print([col.column_name for col in curs.columns('A')])
print([col.column_name for col in curs.columns('A')])
print([col.column_name for col in curs.columns('A')])
print([col.column_name for col in curs.columns('A')])
# Only succeeds 1/3 times for {ODBC Driver 13 for SQL Server}
# Only succeeds 1/3 times for {SQL Server Native Client 11.0}
# Only succeeds 1/2 times for {SQL Server}
The tables of seven characters return an empty result set the first time they are called, then return properly after.
The tables with one character names return empty result sets the first two times they are called, then a proper result set, then repeat failing twice in a row and succeeding once as you make more calls.
Unlike issue #501, I cannot replicate this on Python 3.7.2
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:26 (7 by maintainers)
I have the same issue using Python 3.6.8 on Windows 64-bit and MS-SQL (SQL server 2012). columns() call works on some tables, but not others, and I cannot find any common factor between the ones that work and the ones that don’t.
Makes 4.0.25 pretty much unusable for me. 4.0.24 works fine though,
Possibly this comment should be on #501 though, those two bugs seem pretty similar.
@keitherskine thanks for the workaround. I was completely baffled as to why a MSSQL table (using the SQL Native ODBC driver) wasn’t showing any columns when it’s supposed to have two. Using this workaround to bypass this issue.