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

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.

Related

Python mariadb module does not connect to database on network

I am trying to connect to a mariadb-database on my local network. using Python.
import mariadb
cursor = mariadb.connect(host='192.168.178.77', user='someuser', password='somepass', db='temps')
Output is:
Traceback (most recent call last):
File "/Users/localuser/PycharmProjects/SQL/main.py", line 20, in <module>
cursor = mariadb.connect(host='192.168.178.77', user='someuser', password='somepass', db='temps')
File "/Users/user/.conda/envs/SQL/lib/python3.10/site-packages/mariadb/__init__.py", line 142, in connect
connection = connectionclass(*args, **kwargs)
File "/Users/localuser/.conda/envs/SQL/lib/python3.10/site-packages/mariadb/connections.py", line 86, in __init__
super().__init__(*args, **kwargs)
mariadb.OperationalError: Can't connect to server on '192.168.178.77' (60)
I can connect via Pycharms Database functionality and send SQL Statements.
I also can use DB management tools from that very host and use data without any issue.
It even works from my phone.
This code is the only place where I get an error.
OS is MacOS13.0.1
Thank You!
This happens due to a bug in MariaDB Connector/C. (Issue CONC-612).
The issue was fixed in C/C Version 3.3.3 - which is available via brew:
After
brew update
brew upgrade mariadb-connector-c
connection should work as expected.
I've got the same problem recently. Add port variable and check other. If doesn't help, try mysql-connector-python it works similar. Or install mariadb connector manually

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.

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

Categories