简体   繁体   中英

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. However, when I imported SQL magic and attempted to connect to the server, it failed.

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.

Method 1 (IBM recommended):

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]:

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.

Thank you!

PS If I sounded rude or have offended you somehow, please understand that English is not my mother tongue. :(

Using an existing python library for IBM DB2

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()

Reference1 , Reference2

我遇到了同样的问题,但随后对 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. 30/12/2021

installing the 1.3.9 version of sqlalchemy resolved the issue for me:

!pip install sqlalchemy==1.3.9

You might try to add ;security=SSL :

... @hostname:port/bludb;security=SSL

to the end of your connection string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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