我目前正在尝试设置一个python脚本,该脚本连接到Oracle DB以获取订单排名并计算某些KPI并将其写入专用表。
但是,一旦我尝试连接,Python就会抛出
"接口错误:无法获取Oracle环境句柄"
然而,如果我在执行python脚本之前打开Toad for Oracle并连接到数据库,它确实有效。
from sqlalchemy import create_engine
import pandas as pd
import cx_Oracle as oracle
import sqlalchemy
import os
def main():
### See: https://stackoverflow.com/questions/15797894/named-parameters-in-sql-queries-cx-oracle-ora-01460-unimplemented-or-unreas
os.environ.update([('NLS_LANG', '.UTF8'),('ORA_NCHAR_LITERAL_REPLACE', 'TRUE'),])
engine = create_engine('oracle+cx_oracle://USR:PW@IP/SID')
sql_seasons = read_sql_file("Query.sql")
table = pd.read_sql(sql_seasons, engine)
...
我必须使用Oracle客户端10g(10.2)32位(服务器端:12.1 64位版本)
所以我使用的是cx_Oracle-5.1.2-10g.win32-py2.7 5.1(见https://sourceforge.net/projects/cx-oracle/files/5.1.2/)
使用Python 2.7 32位(CPython)
环境变量:
PATH = c:\oracle\products\10.2.0\bin
ORACLE_HOME = c:\oracle\products\10.2.0
TNS_ADMIN = c:\oracle\network
LD_LIBRARY_PATH = C:\oracle\product\10.2.0\lib
oci.dll可以在c:\ oracle \ products \ 10.2.0 \ bin
找到当我尝试通过cx_Oracle直接连接而不是使用sqlalchemy时,会发生同样的情况 有没有办法解决这个问题?是否有任何其他oracle连接器可靠地允许我通过10g客户端访问? 我已经看到还有与Jython一起使用的zxJDBC连接器。我想避免迁移到java平台。
编辑: 堆栈跟踪下方: 回溯(最近一次调用最后一次):
File "C:\Users\...\File.py", line 83, in <module>
main()
File "C:\Users\...\File.py", line 30, in main
table = pd.read_sql(sql_seasons, engine)
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 416, in read_sql
chunksize=chunksize)
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 1092, in read_query
result = self.execute(*args)
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 983, in execute
return self.connectable.execute(*args, **kwargs)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2063, in execute
connection = self.contextual_connect(close_with_result=True)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2112, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "C:\Python27\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 387, in connect
return _ConnectionFairy._checkout(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\Python27\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Python27\lib\site-packages\sqlalchemy\dialects\oracle\cx_oracle.py", line 794, in connect
*cargs, **cparams)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.InterfaceError: (cx_Oracle.InterfaceError) Unable to acquire Oracle environment handle