question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cursor.columns doesn't return column names

See original GitHub issue

Environment

  • 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:26 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
gjb1002commented, Jan 18, 2019

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.

1reaction
ewongbbcommented, Jul 9, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

return column names from pyodbc execute() statement
When I tried this, I got a TypeError: 'NoneType' object is not iterable. – RufusVS. Sep 17, 2018 at 15:36.
Read more >
Fetching Python Database Cursors by Column Name
Today I got asked if you can index in to rows returned by ibm_db_dbi by column name. While this doesn't come out of...
Read more >
How to write SQL queries with spaces in column names
In this article, we are going to learn how we can write a SQL query with space in the column name. Blanks spaces...
Read more >
sp_describe_cursor_columns (Transact-SQL) - Microsoft Learn
Cursors Returned ; column_name, sysname (nullable), Name assigned to the result set column. The column is NULL if the column was specified ...
Read more >
Get column names from PostgreSQL table using Psycopg2
Example 2: In the second approach, we execute the following SQL command in the cursor.execute() method. “select COLUMN_NAME from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found