I'm trying to connect to an IBM DB2 database from Python. I'm using Python 3.12.10, SQLAlchemy 1.4.54, and Pandas 2.3.2. This is what my code looks like:
import os
import sqlalchemy
import pandas as pd
from keyring import get_credential
if os.name == "nt":
os.add_dll_directory(os.path.join(os.getenv("IBM_DB_HOME"), "bin"))
hostname = #my hostname
port = #my port number
database = #my database
engine = sqlalchemy.create_engine(
f"db2+ibm_db://"
f"{get_credential('my saved credentials', None).username}"
f":{get_credential('my saved credentials', None).password}"
f"@{hostname}:{port}/{database}")
df = pd.read_sql_query(
sql = f"""
--my SQL query
;""",
con = engine)
However, this returns the following error message:
UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.
Does anyone know why this is happening and what I can do to fix it please? I am using a SQLAlchemy engine for the 'con' parameter in pd.read_sql_query, as the error message advises me to do, so I'm not sure what the problem is. I've confirmed that the hostname, port, database name, and my credentials are all correct.
sqlalchemy.create_engine(f"db2+ibm_db://{username}:{password}@{hostname}:{port}/{database}")