我尝试使用以下脚本通过SQLAlchemy连接到db2数据库:( Windows 7 x64,使用库的Python 3.6:ibm_db_sa和ibm_db_2.0.8a,z / OS上的DB2)
import traceback
import ibm_db_sa
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import scoped_session, sessionmaker
if __name__ == '__main__':
try:
Base = automap_base()
connection_string = 'db2+ibm_db://userName:Password@Server.com:6001/DB_0;'
engine = create_engine(connection_string)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
# reflect the tables
Base.prepare(engine, reflect=True)
base_sa = Base.classes
except Exception as err:
print(traceback.format_exc())
engine.close()
不幸的是,我在 Base.prepare(engine,reflect = True)行上获得了以下追溯/错误。这是一个概念证明代码,用于测试我是否确实可以连接到db2数据库并使用SQLAlchemy反映表。
我还尝试运行原始SQL语句,而不是反映具有相同结果的语句:
result = engine.execute(r'SELECT * FROM fooschema.footable;')
TRACEBACK:
Connected to pydev debugger (build 171.4694.67)
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1122, in _do_get
return self._pool.get(wait, self._timeout)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\queue.py", line 145, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 592, in connect
conn = ibm_db.connect(dsn, '', '', conn_options)
SQLCODE=-30073
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
ibm_db_dbi.ProgrammingError: ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/1234567890/Documents/cool_app/db2_is_working.py", line 18, in <module>
Base.prepare(engine, reflect=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\ext\automap.py", line 753, in prepare
autoload_replace=False
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\sql\schema.py", line 3840, in reflect
with bind.connect() as conn:
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2091, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2177, in raw_connection
self.pool.unique_connection, _connection)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
sqlalchemy.exc.ProgrammingError: (ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1122, in _do_get
return self._pool.get(wait, self._timeout)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\queue.py", line 145, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 592, in connect
conn = ibm_db.connect(dsn, '', '', conn_options)
SQLCODE=-30073
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
ibm_db_dbi.ProgrammingError: ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/1234567890/Documents/cool_app/db2_is_working.py", line 18, in <module>
Base.prepare(engine, reflect=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\ext\automap.py", line 753, in prepare
autoload_replace=False
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\sql\schema.py", line 3840, in reflect
with bind.connect() as conn:
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2091, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2177, in raw_connection
self.pool.unique_connection, _connection)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\1234567890\Documents\cool_app\.env\lib\site-packages\ibm_db_dbi.py", line 595, in connect
raise _get_exception(inst)
sqlalchemy.exc.ProgrammingError: (ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: [IBM][CLI Driver] SQL30073N "0x0000" Parameter value "0x0000" is not supported. SQLSTATE=58017\r SQLCODE=-30073
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1234567890\AppData\Roaming\JetBrains\PyCharm 2017.1.4\helpers\pydev\pydevd.py", line 1591, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Users\1234567890\AppData\Roaming\JetBrains\PyCharm 2017.1.4\helpers\pydev\pydevd.py", line 1018, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Users\1234567890\AppData\Roaming\JetBrains\PyCharm 2017.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/1234567890/Documents/cool_app/db2_is_working.py", line 23, in <module>
engine.close()
AttributeError: 'Engine' object has no attribute 'close'
答案 0 :(得分:0)
问题是我没有正确的主机/用户/密码。我通过创建DSN,测试连接然后使用pyodbc进行连接来解决问题。我仍然无法让SQLAlchemy使用连接,但会为此发布一个单独的问题。
更新:SQLAlchemy目前不支持z / OS上的DB2。
答案 1 :(得分:0)
截至2020年8月,SqlAlchemy支持IBM_db,我能够在Z / OS上成功连接和检索数据
from sqlalchemy import Column, Integer, String, MetaData
from sqlalchemy import create_engine
engine = create_engine("db2+ibm_db://userId:password@host:port/db") #create a database engine
from sqlalchemy.ext.declarative import declarative_base
metadata = MetaData(schema='schemaName')
Base = declarative_base(bind=engine, metadata=metadata)
class Customers(Base):
__tablename__ = 'tableName'
CLIENT_ID = Column(Integer, primary_key=True)
CLIENT_NAME = Column(Integer)
CREATED_BY = Column(String)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind = engine)
session = Session()
result = session.query(Customers).limit(2)
for row in result:
print (row.CLIENT_NAME )