[英]How to fix SQLAlchemy connection problem: 'Connection info needed in SQLAlchemy format' when connecting to IBM db2 server hosted on IBM Cloud
So, I've been trying to connect to the IBM DB2 server hosted on IBM cloud for the past few days and managed to connect to it using the provided credentials and the 'ibm_db', 'ibm_db_sa', and the 'ibm_db_dbi' module.因此,过去几天我一直在尝试连接到托管在 IBM 云上的 IBM DB2 服务器,并使用提供的凭证和“ibm_db”、“ibm_db_sa”和“ibm_db_dbi”模块设法连接到它。 However, when I imported SQL magic and attempted to connect to the server, it failed.
但是,当我导入 SQL 魔法并尝试连接到服务器时,它失败了。
I have tried a total of 3 methods: The IBM recommended method, and some other methods I found on the inte.net, which sadly, failed.我一共尝试了3种方法:IBM推荐的方法,以及我在inte.net上找到的其他一些方法,遗憾的是都失败了。
Method 1 (IBM recommended):方法一(IBM推荐):
import ibm_db
import ibm_db_sa
import sqlalchemy
from sqlalchemy import *
%load_ext sql
%sql ibm_db_sa://qcf54xxx:qz^d5stlkbr6lxxx@https://dashdb-txn-sbox-yp-dal09-03.services.dal.bluemix.net:50000/BLUDB
Error:错误:
Connection info needed in SQLAlchemy format, example:
postgresql://username:password@hostname/dbname
or an existing connection: dict_keys([])
invalid literal for int() with base 10: ''
Connection info needed in SQLAlchemy format, example:
postgresql://username:password@hostname/dbname
or an existing connection: dict_keys([])
Method 2:方法二:
from sqlalchemy import create_engine
engine = create_engine('ibm_db_sa://qcf54xxx:qz^d5stlkbr6lxxx@dashdb-txn-sbox-yp-dal09-03.services.dal.bluemix.net:50000/BLUDB')
Error:错误:
Invalid Syntax
Method 3:方法三:
import sqlalchemy
from sqlalchemy import *
import ibm_db_sa
db2 = sqlalchemy.create_engine('ibm_db_sa://qcf54xxx:qz^d5stlkbr6lxxx@dashdb-txn-sbox-yp-dal09-03.services.dal.bluemix.net:50000/BLUDB')
metadata = MetaData()
Error:错误:
Invalid Syntax
The method that succeeded (The method without SQLAlchemy)[Just for your references]:成功的方法(没有SQLAlchemy的方法)【仅供参考】:
import ibm_db
import ibm_db_sa
import ibm_db_dbi
import pandas
#Connects to the IBM database
dsn_hostname = "dashdb-txn-sbox-yp-dal09-03.services.dal.bluemix.net" # e.g.: "dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net"
dsn_uid = "qcf54xxx" # e.g. "abc12345"
dsn_pwd = "qz^d5stlkbr6lxxx" # e.g. "7dBZ3wWt9XN6$o0J"
dsn_driver = "{IBM DB2 ODBC DRIVER}"
dsn_database = "BLUDB" # e.g. "BLUDB"
dsn_port = "50000" # e.g. "50000"
dsn_protocol = "TCPIP" # i.e. "TCPIP"
#DO NOT MODIFY THIS CELL. Just RUN it with Shift + Enter
#Create the dsn connection string
dsn = (
"DRIVER={0};"
"DATABASE={1};"
"HOSTNAME={2};"
"PORT={3};"
"PROTOCOL={4};"
"UID={5};"
"PWD={6};").format(dsn_driver, dsn_database, dsn_hostname, dsn_port, dsn_protocol, dsn_uid, dsn_pwd)
#print the connection string to check correct values are specified
print(dsn)
#DO NOT MODIFY THIS CELL. Just RUN it with Shift + Enter
#Create database connection
try:
conn = ibm_db.connect(dsn, "", "")
print ("Connected to database: ", dsn_database, "as user: ", dsn_uid, "on host: ", dsn_hostname)
except:
print ("Unable to connect: ", ibm_db.conn_errormsg() )
I expect to be able to use %sql to manipulate the database.我希望能够使用 %sql 来操作数据库。
Thank you!谢谢!
PS If I sounded rude or have offended you somehow, please understand that English is not my mother tongue. PS 如果我听起来很粗鲁或以某种方式冒犯了你,请理解英语不是我的母语。 :(
:(
Using an existing python library for IBM DB2 使用现有的IBM DB2 python 库
import sqlalchemy
from sqlalchemy import *
import ibm_db_sa.ibm_db_sa
db2 = sqlalchemy.create_engine('ibm_db_sa://user:password@host.name.com:50000/database')
db2.connect()
metadata = MetaData()
users = Table('STAFF', metadata,
Column('ID', Integer, primary_key = True),
Column('NAME', String(9), nullable = False),
Column('DEPT', Integer, nullable = False),
Column('JOB', String(5), nullable = False)
)
metadata.create_all()
我遇到了同样的问题,但随后对 ibm_db_sa 进行了 pip 安装,然后确保按上述方式导入,然后它就起作用了。
#try this;
%sql ibm_db_sa://my-username:my-password@my-hostname:my-port/my-db-name?security=SSL
#There could be something wrong with DB2, I am failing to long in also when the same code worked last time. #DB2 可能有问题,当相同的代码上次工作时,我也没有渴望。 30/12/2021
30/12/2021
installing the 1.3.9 version of sqlalchemy resolved the issue for me:安装 1.3.9 版本的 sqlalchemy 为我解决了这个问题:
!pip install sqlalchemy==1.3.9 !pip install sqlalchemy==1.3.9
You might try to add ;security=SSL
:您可以尝试添加
;security=SSL
:
... @hostname:port/bludb;security=SSL
to the end of your connection string .到连接字符串的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.