I'm trying to connect to an old database that has its own equally old ODBC driver (which is no longer maintained)
# The driver is 32 bit, so I'm using Python 3.8.7 (32bit)
con = pyodbc.connect(r"Driver={CSI RBM 4.02 ODBC Driver};Dbq=" + database_path + ";Server=localhost")
But I'm getting this error:
[08001] [Microsoft][ODBC Driver Manager] The driver doesn't support the version of ODBC behavior that the application requested (see SQLSetEnvAttr).
According to https://knowledgebase.progress.com/articles/Article/000033360 it means:
This error indicates that an application requested ODBC 3.x behavior however the driver was only ODBC 2.x compliant.
Which I assume means that Python or Pyodbc is using ODBC 3.x and the driver is using 2.x.
So if I've assumed all that correctly, then my question is how can I force Pyodbc to use ODBC 2.x, or is there any other way I can connect to this database using Python?
Any other helpful information would be appreciated too
You can build it from source.
In the /src/pyodbcmodule.cpp file about at the 330 line change SQL_OV_ODBC3 to SQL_OV_ODBC2 then build and install.
I also have and ancient odbc driver what only works with SQL_OV_ODBC2 but hangs every time with SQL_OV_ODBC3.
/src/pyodbcmodule.cpp ~line 330
if (!SQL_SUCCEEDED(SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC2, sizeof(int) ) ) )
{
PyErr_SetString(PyExc_RuntimeError, "Unable to set SQL_ATTR_ODBC_VERSION attribute.");
return false;
}
You can find the source code and documentation for building from sourc here:
https://github.com/mkleehammer/pyodbc/wiki/Building-pyodbc-from-source
python setup.py build
python setup.py install
For driver testing you can use the ODBC Test tool from Microsoft.
You can try out any settings of the ODBC driver without writing any code.
https://learn.microsoft.com/en-us/sql/odbc/odbc-test?view=sql-server-ver15
https://www.microsoft.com/en-us/download/details.aspx?id=21995
Related
I am trying to connect a Firebird database with Python. I already tried it with pyodbc:
import os
import pyodbc
server = '127.0.0.1/3050'
database = 'Databse-Name'
username = 'Username'
password = 'password'
cnxn = pyodbc.connect('DRIVER={Firebird/InterBase(r)
driver};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
I get this error:
OperationalError: ('08004', "[08004] [ODBC Firebird Driver]Unable to connect to data source: library 'gds32.dll' failed to load (-904) (SQLDriverConnect); [08004] [ODBC Firebird Driver]Invalid connection string attribute (0)")
I am not sure why he tries to find 'gds32.dll'. In the ODBC-Connection I used this driver C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\fbclient.dll
I am using Firebird as a 64-bit version, so I am a bit clueless because of the 32 in 'gds32.dll'.
I am not sure, if it is the right way to try it with pyodbc. I am open for other advice.
Has anyone an idea why it is not working?
The fact the error mentions gds32.dll means it tried to load fbclient.dll, and that didn't work. Then it tried to fallback to gds32.dll. The gds32.dll is supported historically, because Firebird was forked from InterBase 22 years ago, and InterBase used the name gds32.dll for its client library. The 64-bit version is also called gds32.dll.
The problem is that, unless the C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\ folder is explicitly on the path, or you configured the CLIENT connection property, that no library is found (or possibly it's 32-bit not 64-bit).
You need a 64-bit fbclient.dll. If that C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\ is really a 64-bit Firebird (then C:\Program Files (x86) is the wrong location), you either need to specify the path of the 64-bit client library in the CLIENT connection property, or you can install it with - from a command prompt started as administrator - instclient i f from a Windows 64-bit Firebird installation, or do a client install using a Firebird installer. Alternatively, you can download the zipkit of a Windows 64-bit Firebird and use its fbclient.dll.
You should also consider using one of the Firebird drivers for Python, instead of using ODBC. You can choose from:
firebird-driver - uses fbclient.dll
FDB - uses fbclient.dll (deprecated and replaced by firebird-driver)
firebirdsql (aka pyfirebirdsql) - a pure Python driver (no native dependencies)
Also, I'm not sure if Gordon's advice about using SQLAlchemy is correct, but I'd recommend investigating that (though below the covers SQLAlchemy will probably use FDB or maybe firebird-driver, so you'd still need a proper 64-bit client library to load).
If you are going to use pandas with a database other than SQLite you should be using SQLAlchemy (ref: here). In your case you would use the sqlalchemy-firebird dialect.
Edit re: comment to original answer
Since we are connecting to localhost we can expect that Firebird has been installed and therefore the client tools are available (which is true for a default install). In that case, the following works on a Windows 8.1 test machine:
import pandas as pd
import sqlalchemy as sa
# note the r"" string
engine = sa.create_engine(r"firebird://SYSDBA:masterkey#localhost/C:\ProgramData\assfinet\assfinet ams.5\Individuell 2022\DB0 - Stand 2022_02-10.FDB")
df = pd.read_sql_query("SELECT * FROM my_table", engine)
although a better approach would be to build the connection URL like this
connection_url = sa.engine.URL.create(
"firebird",
username="SYSDBA",
password="masterkey",
host="localhost",
database=r"C:\ProgramData\assfinet\assfinet ams.5\Individuell 2022\DB0 - Stand 2022_02-10.FDB",
)
engine = sa.create_engine(connection_url)
I am trying to run the code below.
import pandas as pd
from sqlalchemy import create_engine
import urllib
#import pyodbc
params = urllib.parse.quote_plus("DRIVER='{ODBC Driver 17 for SQL Server}';SERVER=server.database.windows.net;DATABASE=my_db;UID=my_id;PWD=my_pwd")
myeng = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
df.to_sql(name="dbo.my_table_name", con=myeng, if_exists='append', index=False)
I get an error when I hit the last line of code. I am getting this error.
DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib ''{ODBC Driver 17 for SQL Server}'' : file not found (0) (SQLDriverConnect)")
I am reading through the documentation here.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html
Everything pretty much makes sense to me, but I'm not sure how to reference the SQL Server driver. When I look at the DOBC setup on my laptop, I see this.
I think this is ok, but I'm actually pushing data to an Azure Data Warehouse (on a server machine, not my local machine). I'm not sure how to check the driver on that DB sitting on the server. Also, I'm not totally sure, but the problem seems to come from either the DRIVER or the SERVER. Basically, I am just looking for some guidance as to how to make this work. Thanks!
To check installed drivers or DSNs on client machine, use the following lists from pyodbc:
# LIST OF INSTALLED DATA SOURCES (DSNs)
print(pyodbc.dataSources())
# LIST OF INSTALLED DRIVERS
print(pyodbc.drivers())
Do note: 32 or 64-bit versions will only appear on the analogous bit-version of your Python installation (i.e., only 32-bit drivers will show on Python 32-bit and similarly for 64-bit). You show a list of 64-bit drivers but may be running Python 32-bit of which none of those are available. Recall Windows maintains two odbc executables usually in below system folders:
C:\Windows\System32\odbcad32.exe (your screenshot)
C:\Windows\SysWOW64\odbcad32.exe
My Softwares:
Python 3.4 -64 bit
PyODBC 64 bit
MS office package Installed (32 bit)
Problem:
Now, I try to access MS Access 2010 installed in my computer using PYODBC. It does not work regardless of what I try. My error is always this:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I already looked at:
pyodbc and ms access 2010 connection error
Cannot connect to Access DB using pyodbc
It is suggested that it is 32-64 bit problem which I am sure it is. Is there some modification that can be done to make it work without installing 32 bit python and 32 bit pyodbc? I checked this website.Using ODBC (32-bit and 64-bit) on 64-bit Windows which explains about accessing the control panel to modify ODBC connnection and/or drivers. However, I don't know much about windows database connection internals to commence some changes.
Is there something that I can do to make it work?
Is there some modification that can be done to make it work without installing 32 bit python and 32 bit pyodbc?
Not really. If you have 32-bit Office installed then you have the 32-bit version of the Access Database Engine (a.k.a. "ACE"), and only 32-bit applications can use it.
The installer for the 64-bit version of the Access Database Engine will abort if it detects 32-bit Office components. There is a way to force the installer to continue, but that is not recommended because it can apparently break Office.
Bottom Line: The "bitness" of your application must match the "bitness" of the installed Access Database Engine. So, practically speaking, your options are
Use 32-bit Python and pyodbc, or
replace your 32-bit Office with the 64-bit version.
When attempting to connect to a PostgreSQL database with ODBC I get the following error:
('08P01', '[08P01] [unixODBC]ERROR: Unsupported startup parameter: geqo (210) (SQLDriverConnect)')
I get this with two different ODBC front-ends (pyodbc for Python and ODBC.jl for Julia), so it's clearly coming from the ODBC library itself. Is there a way to stop it from passing this "geqo" parameter?
An example in pyodbc would be very useful.
Thanks.
The error indicates that the ODBC driver tries to set the geqo parameter in the startup packet, but the PostgreSQL server does not recognize it.
This is bunny because this parameter is exists in all PostgreSQL versions I know, at least down to 7.1.
Is it possible that you are using a fork of PostgreSQL that does not have this parameter?
Another funny thing is that this commit from 2014 removes the geqo setting, so in recent versions of the ODBC driver it should not even be used (unless you specify it explicitly with the Connect Settings setting).
Maybe your problem will vanish if you use a recent ODBC driver version.
Config SSL Mode: allow in ODBC Driver postgres, driver version: 9.3.400
I'm trying to connect to a MS Access Database (.accdb file) via python.
I used pyodbc to do this connection:
import pyodbc
conn = pyodbc.connect("DRIVER = {Microsoft Access Driver (*.mdb, *.accdb)}; DBG=C:\\test_db.accdb")
However, I got the following error:
('IM002, '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I went to the ODBC Data Source Administrator and when I tried to configure or remove the Driver I got the message:
Errors Found:
The specified DSN contains an architecture mismatch between the Driver and Application
I found that this error is provoked by an incompatibility between versions of Windows (windows 7 - 64bit) and Microsoft Access (Office 2010 - 32bits).
I tried to reinstall the driver several times, both with 32 and 64bit versions but the problem wasn't solved.
Could you please help me to solve this problem? Thank you in advance.
You have to make sure the Python version matches the ODBC driver version: 32-bit with 32-bit, 64-bit with 64-bit.
It looks like you have 64-bit Python / pyodbc and 32-bit MS Access.
What you'll need to do is install the 32-bit Python version, and then install pyodbc.
Good luck!