7

I have the following code:

import pyodbc

conn = pyodbc.connect('DSN=QueryBuilder')
cursor = conn.cursor()
stringA = "SELECT GrantInformation.Call FROM GrantInformation"
cursor.execute(stringA)
rows = cursor.fetchall()

Its worked fine for years, but all of a sudden it has stopped working today with the following traceback:

Traceback (most recent call last):
  File "C:/Users/nicholas/Desktop/test.py", line 6, in <module>
    cursor = conn.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'

The code still works in Jupyter Notebook, but no longer works outside i.e. in Pycharm/IDLE

If I run in Pycharm:

print(pyodbc.dataSources())
print(pyodbc.drivers())

I get:

none
none

If I do it in Jupyter Notebook, I get:

{'Visio Database Samples': 'Microsoft Access Driver (*.mdb, *.accdb)', 'dBASE Files': 'Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)', 'Excel Files': 'Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)', 'MS Access Database': 'Microsoft Access Driver (*.mdb, *.accdb)', 'QueryBuilder': 'SQL Server', 'QueryBuilder_Beta': 'SQL Server', 'MIDAS': 'SQL Server', 'Querybuilder_YearEnd': 'SQL Server', 'ResearchFish': 'SQL Server', 'ESRCContacts': 'SQL Server', 'ESRCContactsDEV': 'SQL Server', 'Sample Amazon Redshift DSN': 'Amazon Redshift (x64)'}

['SQL Server', 'SQL Server Native Client 10.0', 'Amazon Redshift (x64)', 'SQL Server Native Client 11.0', 'PostgreSQL ANSI(x64)', 'PostgreSQL Unicode(x64)', 'ODBC Driver 11 for SQL Server', 'ODBC Driver 13 for SQL Server']

I am losing the plot, as I rely on this code a lot for doing all my work, but nothing I am doing is fixing the issue i.e. reinstalling Python, pyodbc etc

2
  • I faced a similar issue and got out of it by upgrading dependencies Commented Feb 20, 2018 at 9:09
  • Thank you Pubudu. That's useful to know if I go back to 3.6! Commented Feb 20, 2018 at 9:32

2 Answers 2

4

conn is None, so you must have failed to acquire a connection. Presumably the notebook where this is working has "QueryBuilder" set up correctly, and wherever else you have tried to use this does not. I'm not familiar with that name specifically, but you may need to set up the data source, or you may need to install the appropriate ODBC driver.

You should be able to look at pyodbc.dataSources() and/or pyodbc.drivers() to get more details about what data sources are configured and what drivers are installed, and see what the difference is between your systems.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, thank you. I will check this out. The strange thing it was working fine yesterday and not today (and a program that I converted from Python to an .exe file is working fine connecting the dsn link), it is literally just in Python that is not on the notebook.
They are both pointing towards the same DSN, so super confused now. Ill have to bring my personal laptop in and test it.
4

Surprised no one has noticed this, but it appears that there are issues with Python 3.6 and pyodbc... I have gone back to 3.5 and it works fine.

1 Comment

how to make it work under 3.10.5 in windows 10?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.