- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
简短版本: 请告诉我如何通过 SQLAlchemy 连接到 AS/400。
长版
我的最终目标是连接来自 SQL Server 和 AS/400 的数据以在 Flask Python 应用程序中显示。我的方法是将每个数据库中的数据获取到 Pandas 数据帧中,然后可以将其连接并输出为 JSON。如果有人有更好的方法,请随时发表评论。我尝试执行此操作的方式的问题是 Pandas.read_sql_query()
依赖于 SQLAlchemy,并且事实证明让 SQLAlchemy 与 AS/400 一起使用非常困难。
pyodbc
和 ibm_db_sa
。 没有sqlalchemy
,pyodbc
工作得很好:
CONNECTION_STRING = (
"driver={iSeries Access ODBC Driver};"
"system=ip_address;"
"database=database_name;"
"uid=username;"
"pwd=password;"
)
pyodbc.connect(CONNECTION_STRING)
# Queries work fine after this.
我阅读了这些资源以及其他资源,并尝试应用他们的技术:
下面是我收集的一些失败的尝试和相应的错误消息。我不知道第一部分要放什么(“something+something//...
”),要指定哪个端口(446?8471?其他什么?什么都没有?),是否要使用服务器的名称或 IP 地址,或者是否使用 create_engine()
的连接字符串样式参数,所以我一直在尝试我能想到的每种组合。我尝试按照上面第二个链接中的建议修改 AS400Dialect_pyodbc
类,之后我尝试再次重新运行一些失败的尝试。我可能会继续尝试,但此时我只是在原地踏步。
from sqlalchemy import create_engine
CONNECTION_STRING = (
"driver={iSeries Access ODBC Driver};"
"system=ip_address;"
"database=database_name;"
"uid=username;"
"pwd=password;"
)
create_engine('ibm_db_sa+pyodbc://username:password@ip_address:446/database_name').connect()
Exception has occurred: sqlalchemy.exc.InterfaceError (pyodbc.InterfaceError) ('IM002', u'[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: http://sqlalche.me/e/rvf5) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 43, in
create_engine('ibm_db_sa://username:password@ip_address:446/database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL30061N The database alias or database name "database_name " was not found at the remote node. SQLSTATE=08004\r SQLCODE=-30061 (Background on this error at: http://sqlalche.me/e/e3q8) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 43, in
create_engine('ibm_db_sa://username:password@server_name:446/database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL1336N The remote host "server_name" was not found. SQLSTATE=08001\r SQLCODE=-1336 (Background on this error at: http://sqlalche.me/e/e3q8create_engine('ibm_db_sa://username:password@ip_address:446/server_name.database_name').connect()
create_engine('ibm_db_sa://username:password@ip_address:446/server_name.database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL30061N The database alias or database name "server_name.database_name " was not found at the remote node. SQLSTATE=08004\r SQLCODE=-30061 (Background on this error at: http://sqlalche.me/e/e3q8)
create_engine('db2+ibm_db://username:password@ip_address:446/server_name.database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL30061N The database alias or database name "server_name.database_name " was not found at the remote node. SQLSTATE=08004\r SQLCODE=-30061 (Background on this error at: http://sqlalche.me/e/e3q8) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 45, in
create_engine('db2+ibm_db://username:password@ip_address:446/database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL30061N The database alias or database name "database_name " was not found at the remote node. SQLSTATE=08004\r SQLCODE=-30061 (Background on this error at: http://sqlalche.me/e/e3q8) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 45, in
create_engine('db2+ibm_db://username:password@ip_address/database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "ip_address". Communication function detecting the error: "connect". Protocol specific error code(s): "10061", "", "". SQLSTATE=08001\r SQLCODE=-30081 (Background on this error at: http://sqlalche.me/e/e3q8)
create_engine('db2+ibm_db://username:password@server_name:446/database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL1336N The remote host "server_name" was not found. SQLSTATE=08001\r SQLCODE=-1336 (Background on this error at: http://sqlalche.me/e/e3q8)
create_engine('db2+ibm_db://username:password@server_name/database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL1336N The remote host "server_name" was not found. SQLSTATE=08001\r SQLCODE=-1336 (Background on this error at: http://sqlalche.me/e/e3q8)
create_engine('db2+pyodbc://username:password@ip_address:446/database_name').connect()
Exception has occurred: sqlalchemy.exc.InterfaceError (pyodbc.InterfaceError) ('IM002', u'[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: http://sqlalche.me/e/rvf5) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 45, in
create_engine('db2://username:password@ip_address:446/database_name').connect()
Exception has occurred: sqlalchemy.exc.OperationalError (ibm_db_dbi.OperationalError) ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL30061N The database alias or database name "database_name " was not found at the remote node. SQLSTATE=08004\r SQLCODE=-30061 (Background on this error at: http://sqlalche.me/e/e3q8) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 45, in
quoted = urllib.quote_plus(CONNECTION_STRING)
create_engine('ibm_db_sa+pyodbc:///?odbc_connect={}'.format(quoted)).connect()
Unable to open 'hashtable_class_helper.pxi': File not found (file:///c:/git/dashboards/pandas/_libs/hashtable_class_helper.pxi).
quoted = urllib.quote_plus(CONNECTION_STRING)
create_engine('ibm_db_sa:///?odbc_connect={}'.format(quoted)).connect()
Exception has occurred: sqlalchemy.exc.InterfaceError (ibm_db_dbi.InterfaceError) ibm_db_dbi::InterfaceError: connect expects the first five arguments to be of type string or unicode (Background on this error at: http://sqlalche.me/e/rvf5) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 43, in
quoted = urllib.quote_plus(CONNECTION_STRING)
create_engine('ibm_db:///?odbc_connect={}'.format(quoted)).connect()
Exception has occurred: sqlalchemy.exc.NoSuchModuleError Cant load plugin: sqlalchemy.dialects:ibm_db
quoted = urllib.quote_plus(CONNECTION_STRING)
create_engine('db2:///?odbc_connect={}'.format(quoted)).connect()
Exception has occurred: sqlalchemy.exc.InterfaceError (ibm_db_dbi.InterfaceError) ibm_db_dbi::InterfaceError: connect expects the first five arguments to be of type string or unicode (Background on this error at: http://sqlalche.me/e/rvf5) File "C:\Git\dashboards\web_app\pandas db2 test.py", line 45, in
quoted = urllib.quote_plus(CONNECTION_STRING)
create_engine('db2+ibm_db:///?odbc_connect={}'.format(quoted)).connect()
Exception has occurred: sqlalchemy.exc.InterfaceError (ibm_db_dbi.InterfaceError) ibm_db_dbi::InterfaceError: connect expects the first five arguments to be of type string or unicode (Background on this error at: http://sqlalche.me/e/rvf5)
quoted = urllib.quote_plus(CONNECTION_STRING)
create_engine('db2+ibm_db_sa:///?odbc_connect={}'.format(quoted)).connect()
Exception has occurred: sqlalchemy.exc.NoSuchModuleError Cant load plugin: sqlalchemy.dialects:db2.ibm_db_sa
最佳答案
我终于让它工作了,虽然有点尴尬。我在项目中创建了一个空白文件,以平息我收到的这条消息,以响应我的问题中显示的其中一次尝试:
Unable to open
'hashtable_class_helper.pxi'
: File not found (file:///c:/git/dashboards/pandas/_libs/hashtable_class_helper.pxi
).
(我的项目文件夹是 C:/Git/dashboards
,所以我创建了路径的其余部分。)
有了该文件,下面的代码现在对我有用。 engine.connect()
有效,但我运行了一个实际查询以进一步验证它是否有效。根据记录,无论 ibm_db_sa
是否有效,它似乎都有效。模块已按照我的问题中的链接之一的建议进行修改,因此我建议保留该模块。请注意,虽然它们不是直接导入的,但您需要安装这些模块:pyodbc
, ibm_db_sa
,并且可能future
(我忘了)。
import urllib
import pandas as pd
from sqlalchemy import create_engine
CONNECTION_STRING = (
"driver={iSeries Access ODBC Driver};"
"system=ip_address;"
"database=database_name;"
"uid=username;"
"pwd=password;"
)
SQL= """\
SELECT
MPBASE AS BASEPA,
COALESCE(SUM(MPQTY), 0) AS PWIP
FROM FUTMODS.MPPROD
WHERE MPOPT <> '*'
GROUP BY MPBASE
"""
quoted = urllib.quote_plus(CONNECTION_STRING)
engine = create_engine('ibm_db_sa+pyodbc:///?odbc_connect={}'.format(quoted))
df = pd.read_sql_query(
SQL,
engine,
index_col='basepa'
)
print df
关于python - 如何使用 SQLAlchemy 访问 AS/400?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54241513/
我是一名优秀的程序员,十分优秀!