Try to connect a Firebird 3.05 Database to Python (pandas) - python

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)

Related

How can I confirm that I have the correct ODBC Driver 17 for SQL Server?

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

Connecting Python 3.x to Oracle DB

I am unable to connect to our enterprise Oracle Db using python 3/cx_Oracle.
Installed are:
python 3 -32 bit
cx_Oracle
Oracle Client 12.1.0.2.0
My connection string attempt is:
import cx_Oracle
conn = cx_Oracle.connect(user='user', password='pwd', dsn='working_dsn')
My PATH variable includes the direct path to my working Oracle library (works using SQL Dev
Error message is:
cx_Oracle.DatabaseError: DPI-1050: Oracle Client library is at version 0.0 but must be at version 11.2 or higher
I have researched the Orcale installation instructions and have found no way to connect. I have previously tried with no success, had my computer reimaged and Oracle reinstalled to ensure only one version of Oracle and still no success. I need to move from R to Python and this is the last piece I need to make the switch. I am able to connect with R using JDBC driverclass/dbConnect.
If cx_Oracle wont work, is there another option for connecting to Oracle from Python3?
Any thoughts suggestions or places to look? Other connection types used?
Thanks in advance.
pip3 install cx_Oracle
first method:
db = cx_Oracle.connect('root/root#localhost: 1523/orcl')
Second method:
db = cx_Oracle.connect('root/root#localhost: 1523/orcl')
Third method
makedsn(IP/HOST, PORT, TNSNAME)
dsn = cx_Oracle.makedsn('localhost','1523','orcl')
db = cx_Oracle.connect('root','root',dsn)
The error you are getting suggests that you have an older version of the Oracle client installed on your machine. Search the directories of your PATH environment variable for OCI.DLL. If you find an older version you'll need to remove or rename it -- just be aware that whatever application put the file there will stop working!
Another possibility is to go to a command prompt and do the following
PATH=my_path_to_instant_client;%PATH%
python test_connect.py
Finally, make sure that if your Python is 32-bit, so is your instant client installation, and if your Python is 64-bit, make sure that your instant client installation is also 64-bit.

Python and Oracle DB - "Error DPI-1050: Oracle Client library must be at version 11.2 or higher"

Some days ago, I was asked to develop a Python application capable of connecting to a Oracle DB. Since I already have an Oracle client installed (version 12.2.0), I just pip installed cx_Oracle and tried to establish a connection using below code:
import pandas as pd
import cx_Oracle
connection = cx_Oracle.connect('username/password#service_as_described_in_tnsnames.ora')
cur=connection.cursor()
input("Press Enter to continue...")
cur.execute('select* from MY_PRETTY_TABLE')
for line in cur:
print()
cur.close()
connection.close()
But when trying to run it, I got the error "DPI-1050: Oracle Client library must be at version 11.2 or higher". After googling it, I found this answer, and tried to change my code to:
my_dsn = cx_Oracle.makedsn("host",port,sid="sid")
connection = cx_Oracle.connect(user="user", password="password", dsn=my_dsn)
cursor = connection.cursor()
querystring = "SQL query"
cursor.execute(querystring)
But still, same error. It's important to notice that I have already used Oracle DB client in this same machine, to connect a DB with Power BI.
Also, if it can be helpful, my paths are setted as:
C:\instantclient_12_1
C:\Users\oracle2\product\12.1.0\client_1
C:\Users\oracle2\product\12.1.0\client_1\bin
C:\Users\oracle\product\12.2.0\dbhome_1\bin
That error implies that you have another older version of the Oracle client installed somewhere earlier in your PATH. You should do a search for OCI.DLL on your machine (using where.exe or the dir command) and either move or remove any unnecessary copies or adjust PATH as needed. Some older applications stuffed OCI.DLL in C:\Windows\System32 improperly, for example.
This problem had to do with Oracle Instant client Version 19.3.0.0.0.
I uninstalled it and installed the previous version Oracle Instant client 12.2.0.1.0 to and it worked.
https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Important things to do before you try above:
From Windows command prompt
c:> where oci.dll
make sure you find only one entry and remove the redundant ones.
Ensure you set the environment path to the newly installed client.

Finding the right DRIVER string for pyodbc

How do I find which DRIVER string to use with pyodbc? I'm trying to connect to an Oracle database.
The connection documentation isn't very specific about how to go about this.
I'm on OSX 10.9.
At first you must have Oracle drivers installed on your machine. Then, on your system you can create DSN (Data Source Name). I see that there if free OSX ODBC manager. I hope you can create DSN with it and then you can use it like:
pyodbc.connect('DSN=my_dsn;uid=username;pwd=secretpassword')
If this works well and you want to omit DSN you can use more fancy Oracle connect string for ODBC which be found at: http://www.connectionstrings.com/oracle/

cx_oracle and oracle 7?

At work we have Oracle 7. I would like to use python to access the DB.
Has anyone done that or knows how to do it?
I have Windows XP, Python 2.6 and the cx_oracle version for python 2.6
However, when I try to import cx_oracle i get the following error:
ImportError: DLL load failed the module could not be found
Any help is appreciated!
Matt
cx_Oracle is currently only being provided with linkage to the 9i, 10g, and 11i clients. Install one of these clients and configure it to connect to the Oracle 7 database using the proper ORACLE_SID.
Make sure you have the location of the oracle .dll (o files set in your PATH environment variable. The location containing oci.dll should suffice.
I was running into that same problem at work. I finally dropped trying to use cx_Oracle and went with adodbapi. It worked with Oracle 8.
If you have ODBC configured then you can use it. It is available with ActivePython or as win32 extensions. You will obtain connection with:
connection = odbc.odbc('db_alias/user/passwd')
Optionally you can use Jython and thin JDBC client. Instalation of client is not required. With Jython you have access to db via db url:
db = DriverManager.getConnection(db_url, usr, passwd)
where db_url looks like:
jdbc:oracle:thin:user/passwd#machine_ip:port:dbname

Categories