简体   繁体   English

使用 SQLAlchemy 连接到 IBM DB2 数据库

[英]Connect to IBM DB2 database using SQLAlchemy

I am trying to connect to a cloud based IBM DB2 database using SQLAlchemy. Using the base ibm_db python library this works:我正在尝试使用 SQLAlchemy 连接到基于云的 IBM DB2 数据库。使用基本的 ibm_db python 库可以正常工作:

connection_string = 'DRIVER={DB2};DATABASE=BLUDB;HOSTNAME=host.databases.appdomain.cloud;PORT=port;PROTOCOL=TCPIP;UID=user;PWD=pass;SECURITY=SSL;'
conn = ibm_db.connect(connection_string, '', '')

However, trying to do the same using SQLAlchemy, the connect part just hangs/timeouts但是,尝试使用 SQLAlchemy 执行相同操作时,连接部分只是挂起/超时

from sqlalchemy import create_engine
import ibm_db_sa

user = "user"
password = "pass"
host = "host.databases.appdomain.cloud"
port = "port"
database = "BLUDB"

db2_connection_string = (
    f'db2+ibm_db://{user}:{password}@{host}:{port}/{database}'
    ':SECURITY=SSL'
)

engine = create_engine(db2_connection_string)
connection = engine.connect() # timeouts here

Any suggestions?有什么建议么?

Only use a connection string like this:只使用这样的连接字符串:

db2_connection_string = (
    f'db2://{user}:{password}@{host}:{port}/{database}'
    ':SECURITY=SSL;PROTOCOL=TCPIP;'
)

In this line of code in ghstats.ps is a working sample. ghstats.ps 中的这一行代码是一个工作示例。 The code is from this IBM Cloud solution tutorial which has a Python app with SQLAlchemy connecting to Db2 on Cloud.代码来自这个IBM Cloud 解决方案教程,它有一个 Python 应用程序,其中 SQLAlchemy 连接到 Cloud 上的 Db2。

As alternative, you can also try ibm_db_sa as driver prefix instead of db2 .作为替代方案,您也可以尝试将 ibm_db_sa作为驱动程序前缀而不是db2 Make sure to import the right modules (ibm_db, ibm_db_sa).确保导入正确的模块(ibm_db、ibm_db_sa)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM