Issue in accessing Microsoft Access file from Python using pyodbc module - python

I am unable to access a Microsoft Access file (.accdb) from Python using the pyodbc module. Below is the patch of code I am using. I have checked my version of Python as well as the Access DB, both are 64-bit.
cnxn = pyodbc.connect('DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ='+'{};Uid={};Pwd={};'.format(db_file, user, password))
I am getting the following error:
InterfaceError Traceback (most recent call last)
in ()
----> 1 cnxn = pyodbc.connect('DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ='+'{};Uid={};Pwd={};'.format(db_file, user, password))
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

Related

Installing pyodbc seems to work, but python cannot find the

I was able to install pyodbc just fine it seems:
pip install pyodbc-4.0.32-cp310-cp310-win_amd64.whl
Processing c:\users\name\appdata\local\programs\python\python310\scripts\pyodbc-4.0.32-cp310-cp310-win_amd64.whl
Installing collected packages: pyodbc
Successfully installed pyodbc-4.0.32
But when I run the code, it fails with the generic error
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver
Manager] Data source name not found and no default driver specified
(0) (SQLDriverConnect)')
Here's the code:
import pyodbc
print("Drivers")
print (pyodbc.drivers())
print ("Sources")
sources = pyodbc.dataSources()
keys = sources.keys()
for key in keys:
print (key)
conn_str = (r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=C:\pyfun\TradingHubOutput.accdb;')
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
Here's the output:
Drivers
['SQL Server']
Sources
Traceback (most recent call last):
File "C:\pyfun\playing with DB.py", line 14, in <module>
conn = pyodbc.connect(conn_str)
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
So only one driver. No data sources listed. pyodbc installed ok - rerunning the command gives a confirmation it is install and only prompts for a forced reinstall.
What's going on?

Pyodbc and Microsoft Access connecting Error

I've been trying to connect to Microsoft Access (64bit) with python 3.7.2 (64bit) via pyodbc. The code:
import pyodbc
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\database accdb;')
cursor = conn.cursor()
cursor.execute('select * from Map Generation')
for row in cursor.fetchall():
print (row)
Produces the error message:
Traceback (most recent call last):
File "E:\NEA\ACCESS TESTING.py", line 3, in <module>
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\database accdb;')
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I've checked some posts, and most suggest to download the Microsoft Access Database Engine 2010 Redistributable, which I also tried. As this post suggests https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Microsoft-Access to check if Microsoft Access driver is available in python environment the following code can be run:
import pyodbc
[x for x in pyodbc.drivers() if x.startswith('Microsoft Access Driver')]
For me this still produces nothing. Is there something I need to do to link the Microsoft Access Driver to my python environment?

How to extract a columns content in Microsoft Access Database using python

I am just starting to learn python and was wondering if someone could help me pull column data using python? I have been researching for about 3 hours.
code:
import pyodbc
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DBQ=C:\Users\jgp22\Documents\Database11.accdb;')
cursor = conn.cursor()
cursor.execute('select * from information')
for row in cursor.fetchall():
print(row)
error:
Traceback (most recent call last):
File "C:\Users\jgp22\Desktop\Python\Data.py", line 3, in <module>
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DBQ=C:\Users\jgp22\Documents\Database11.accdb;')
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified (0)
(SQLDriverConnect)')
error #2:
Traceback (most recent call last):
File "C:\Users\jgp22\Desktop\Python\GrabData.py", line 3, in <module>
conn = pyodbc.connect(r'DSN=MAD')
pyodbc.Error: ('HY000', "[HY000] [Microsoft][ODBC Microsoft Access Driver]
Cannot open database '(unknown)'. It may not be a database that your
application recognizes, or the file may be corrupt. (-1028)
(SQLDriverConnect); [HY000] [Microsoft][ODBC Microsoft Access Driver] Cannot
open database '(unknown)'. It may not be a database that your application
recognizes, or the file may be corrupt. (-1028)")
Create a Named System DSN
Go to ODBC Admin (make sure its the 64-bit one)
Click on System DSN
Create named DSN that points to 'C:\Users\jgp22\Documents\Database11.accdb' in this example lets use myDsn
Use the connection string DSN=myDsn where myDsn is the name you gave in step 3. Don't worry about the driver info or path. All of that is stored with the DSN record on the system.
ODBC Data Source Administrator Paths
To setup DSN for 32-bit application you must use:
%WINDIR%\SysWOW64\odbcad32.exe
and for 64-bit application you must use:
%WINDIR%\System32\odbcad32.exe
From https://robertoschiabel.wordpress.com/2008/02/28/windows-x64-32bit-odbc-vs-64bit-odbc/amp/
Install the 64-bit Access DB Driver
The driver for accessing accdb files might not be available on your system. In that case, you are going to need to install the driver from Microsft. The accdb driver is available for download from Microsoft here:
https://microsoft.com/en-US/download/details.aspx?id=13255
Use 32-bit Python (if Access DB driver isn't available in 64-bit)
Another alternative if you have the 32-bit accdb ODBC driver already installed on your system is to use the 32-bit version of Python. An example of this type of environment might be if you had a 32-bit version of Office installed already.
Install the 32-bit version of Windows Python 3.7.0 from:
https://www.python.org/ftp/python/3.7.0/python-3.7.0.exe
When using the 32-bit version of python, you should access the DSNs under the 32-bit ODBC Data Source Administrator.
Connect to the Named DSN
import pyodbc
conn = pyodbc.connect(r'DSN=myDsn')

How to connect to Netezza/PureData for Analytics using Python

I am trying to connect to Netezza using python's pyodbc.
Here is my code:
import pyodbc
print pyodbc.dataSources()
conn = pyodbc.connect("DRIVER={NetezzaSQL};SERVER=netezza;PORT=<>;DATABASE=SYSTEM;UID=<>;PWD=<>;",ansi=True)
The error that i am getting is:
Error Traceback (most recent call
last) in ()
1 import pyodbc
2 print pyodbc.dataSources()
----> 3 conn = pyodbc.connect("DRIVER={NetezzaSQL};SERVER=netezza;PORT=5480;DATABASE=SYSTEM;UID=ins_etl_usr_qa;PWD=password;",ansi=True)
4 #cnxn = pyodbc.connect("DSN=MSSQL-PYTHON")
5 #conn = pyodbc.connect("DRIVER={NetezzaSQL};SERVER=server.example.com;DATABASE=mydatabase;UID=myusername;PWD=password",
ansi=True)
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib
'NetezzaSQL' : file not found (0) (SQLDriverConnect)")
The error say that it can find NetezzaSQL library so you have to make sure that you have that library installed and accessible.
Accessible in Linux would be having the library installed in the default location for libraries or setting LD_LIBRARY_PATH environment variable to point to the folder where you have the library.

PyODBC "Image not found (0) (SQLDriverConnect)"

I'm trying to use PyODBC to connect to an Access database. It works fine on Windows, but running it under OS X I get—
Traceback (most recent call last):
File "", line 1, in
File "access.py", line 10, in init
self.connection = connect(driver='{Microsoft Access Driver (.mdb)}', dbq=path, pwd=password)
pyodbc.Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen({Microsoft Access Driver (.mdb)}, 6): image not found (0) (SQLDriverConnect)')
Do I have to install something else? Have I installed PyODBC wrong?
Thanks
pyodbc allows connecting to ODBC data sources, but it does not actually implements drivers.
I'm not familiar with OS X, but on Linux ODBC sources are typically described in odbcinst.ini file (location is determined by ODBCSYSINI variable).
You will need to install Microsoft Access ODBC driver for OS X.

Categories