通过 SQLAlchemy 的 `create_engine()` 访问 Oracle 需要更多设置,不是吗?
Accessing Oracle via SQLAlchemy's `create_engine()` requires more settings, is not it?
我正在通过 FastAPI 开发一个 API。它旨在获取输入,从数据库 (Oracle) 中检索数据,并提供经过处理的输出。
这是一个使用 Oracle 引擎执行查询的函数:
from sqlalchemy import exc, create_engine, inspect
def db_connection_sqlalchemy(your_query):
db_config = config['database'] # configurations from a yml-file
try:
engine = create_engine("{0}+{1}://{2}:{3}@{4}".format(db_config['dialect'] #'oracle',
db_config['sql_driver'] #'cx_oracle',
db_config['username'],
db_config['password'],
db_config['network_alias']),
max_identifier_length=30,
encoding="utf8")
inspector = inspect(engine)
conn = engine.connect()
rows = conn.execute(your_query)
rows = rows.fetchmany()
result = [dict(row.items()) for row in rows]
conn.close()
return result
except exc.SQLAlchemyError as e:
error = str(e.__dict__['orig'])
return error
它工作正常。然而,为了设置它,我遇到了几个问题。
问题 1. 通过 pip install SQLAlchemy
安装 SQLAlchemy 后,我遇到了这个错误:
NoSuchModuleError: Can't load plugin:
sqlalchemy.dialects:oracle.cx_oracle
即使我认为 “我不再需要导入 cx_Oracle”,基于 this answer。我决定安装 pip install cx_Oracle
然后我遇到了下一个问题
问题 2. 每次我尝试执行 SQL 查询时,我都会看到此错误:
"DPI-1047: Cannot locate a 64-bit Oracle Client library:
"C:\oracle.2.0\x86\bin\oci.dll is not the correct
architecture". See
https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html
for help"
所以,为了克服这个问题,this answer 帮助了我。
实际上到目前为止一切正常,但是在处理 SQLAlchemy 时是否仍然可以避免那些额外的步骤(安装 cx-Oracle 库,获取 dll 文件)或者这些是使用 Alchemy 时的典型陷阱虚拟环境中的 Oracle 数据库?
我有一个虚拟环境,通过 Anaconda 和 Python 3.7.10 on Windows 10.
我总是单独安装 cx_Oracle - 在我的 pip install
中添加一个词“cx_Oracle”没什么大不了的。关于引用的措辞:导入(在 运行 脚本中)与安装(在系统中)不同。
您始终需要安装 Instant Client 并为其设置 PATH,或者在您的代码中调用 cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries")
(参见 here)。
获取大量行时,调整arraysize
和prefetchrows
,参见Tuning Fetch Performance。
我正在通过 FastAPI 开发一个 API。它旨在获取输入,从数据库 (Oracle) 中检索数据,并提供经过处理的输出。
这是一个使用 Oracle 引擎执行查询的函数:
from sqlalchemy import exc, create_engine, inspect
def db_connection_sqlalchemy(your_query):
db_config = config['database'] # configurations from a yml-file
try:
engine = create_engine("{0}+{1}://{2}:{3}@{4}".format(db_config['dialect'] #'oracle',
db_config['sql_driver'] #'cx_oracle',
db_config['username'],
db_config['password'],
db_config['network_alias']),
max_identifier_length=30,
encoding="utf8")
inspector = inspect(engine)
conn = engine.connect()
rows = conn.execute(your_query)
rows = rows.fetchmany()
result = [dict(row.items()) for row in rows]
conn.close()
return result
except exc.SQLAlchemyError as e:
error = str(e.__dict__['orig'])
return error
它工作正常。然而,为了设置它,我遇到了几个问题。
问题 1. 通过 pip install SQLAlchemy
安装 SQLAlchemy 后,我遇到了这个错误:
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:oracle.cx_oracle
即使我认为 “我不再需要导入 cx_Oracle”,基于 this answer。我决定安装 pip install cx_Oracle
然后我遇到了下一个问题
问题 2. 每次我尝试执行 SQL 查询时,我都会看到此错误:
"DPI-1047: Cannot locate a 64-bit Oracle Client library: "C:\oracle.2.0\x86\bin\oci.dll is not the correct architecture". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help"
所以,为了克服这个问题,this answer 帮助了我。
实际上到目前为止一切正常,但是在处理 SQLAlchemy 时是否仍然可以避免那些额外的步骤(安装 cx-Oracle 库,获取 dll 文件)或者这些是使用 Alchemy 时的典型陷阱虚拟环境中的 Oracle 数据库?
我有一个虚拟环境,通过 Anaconda 和 Python 3.7.10 on Windows 10.
我总是单独安装 cx_Oracle - 在我的 pip install
中添加一个词“cx_Oracle”没什么大不了的。关于引用的措辞:导入(在 运行 脚本中)与安装(在系统中)不同。
您始终需要安装 Instant Client 并为其设置 PATH,或者在您的代码中调用 cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries")
(参见 here)。
获取大量行时,调整arraysize
和prefetchrows
,参见Tuning Fetch Performance。