How to extract a columns content in Microsoft Access Database using python - 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')

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?

Issue in accessing Microsoft Access file from Python using pyodbc module

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)')

isql works but python connection fails

I'm trying to hook up odbc to work on my Mac with python via pyodbc. I've got things set up such that
isql -v <my Connection>
works just file, however when I'm in a python shell and I try:
import pyodbc
pyodbc.connect('DSN=<my Connection>;UID=<username>;PWD=<password>
I get the cryptic:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('HY000', '[] (202) (SQLDriverConnect)')
What can I do to debug this and determine what steps I can take next to get odbc working? Previously, I had odbc installed and was getting "no driver source name" messages in python, and after moving the odbc.ini and odbcinst.ini file to /etc, I now get the error above. Any ideas? Thanks for your time.
Are you trying to connect to SQL Server? Give this a try:
DRIVER={FreeTDS};SERVER=<yourserver.example.com>;PORT=1433;DATABASE=<your DB>;UID=<your username>;<your password>;TDS_Version=7.2;
Hopefully, that'll work for you. The TDS version depends on what version of SQL Server you're using. See here:
http://www.freetds.org/userguide/choosingtdsprotocol.htm
7.2 also works for 2012 + 2014.
Regards,
-Tim

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