我有一个看起来像这样的连接字符串
con_str = "myuser/mypass@oracle.sub.example.com:1521/ora1"
Run Code Online (Sandbox Code Playgroud)
ora1我的数据库的SID 在哪里.在SQL Developer中使用此信息工作正常,这意味着我可以毫无问题地连接和查询.
但是,如果我尝试使用此字符串连接到Oracle,则会失败.
cx_Oracle.connect(con_str)
DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
Run Code Online (Sandbox Code Playgroud)
ora1但是,如果是服务名称,则此连接字符串格式有效.
我看到其他问题似乎与我的问题相反(它适用于SID,但不适用于服务名称)
使用cx_Oracle,SID而不是服务名称连接到Oracle的正确方法是什么?如何在不调整TNSNAMES.ORA文件的情况下执行此操作?我的应用程序在内部分发给许多用户,并且TNSNAMES在处理没有Windows机器管理员权限的用户时,对文件进行更改并不理想.此外,当我使用服务名称时,我根本不需要触摸此文件,并希望它保持这种方式.
如何从Python访问Oracle?我已下载了一个cx_Oracle msi安装程序,但Python无法导入该库.
我收到以下错误:
import cx_Oracle
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import cx_Oracle
ImportError: DLL load failed: The specified module could not be found.
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何帮助.
如何通过IP地址连接到远程服务器,TOAD,SqlDeveloper只能通过IP地址,用户名,SID和密码连接到数据库?
每当我尝试指定IP地址时,它似乎都是在本地使用它.
换句话说,如何将cx_Oracle.connect()的字符串格式化为非本地数据库?
之前的帖子列为了通过cx_Oracle模块连接到Oracle的答案,其中包含以下代码:
#!/usr/bin/python
import cx_Oracle
connstr='scott/tiger'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()
curs.execute('select * from emp')
print curs.description
for row in curs:
print row
conn.close()
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Python 2.7.2和Oracle 11g安装构建cx_Oracle,但是构建的cx_Oracle.so找不到libclntsh.so.11.1,因此在Python中导入cx_Oracle失败.
/mypath/cx_Oracle-5.1.1/build/lib.linux-x86_64-2.7-11g]$ ldd cx_Oracle.so
libclntsh.so.11.1 => not found
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002ae9be290000)
libc.so.6 => /lib64/libc.so.6 (0x00002ae9be4ab000)
/lib64/ld-linux-x86-64.so.2 (0x000000389b600000)
Run Code Online (Sandbox Code Playgroud)
我的Oracle客户端安装目录中有libclntsh.so.11.1:
/apps/oracle/client/11.2.0.1/home1/lib]$ ls -l libclntsh.so*
libclntsh.so -> /apps/oracle/client/11.2.0.1/home1/lib/libclntsh.so.11.1
libclntsh.so.11.1
Run Code Online (Sandbox Code Playgroud)
并且cx_Oracle setup.py正在挑选这个lib目录:
/mypath/cx_Oracle-5.1.1]$ python2.7 setup.py build
/apps/oracle/client/11.2.0.1/home1/
running build
running build_ext
building 'cx_Oracle' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/apps/oracle/client/11.2.0.1/home1/rdbms/demo -I/apps/oracle/client/11.2.0.1/home1/rdbms/public -I/apps/bweb/python-2.7.2/include/python2.7 -c cx_Oracle.c -o build/temp.linux-x86_64-2.7-11g/cx_Oracle.o -DBUILD_VERSION=5.1.1
In file included from /apps/oracle/client/11.2.0.1/home1/rdbms/public/oci.h:3024,
from cx_Oracle.c:10:
/apps/oracle/client/11.2.0.1/home1/rdbms/public/ociap.h:10788: warning: function declaration isn't a prototype
/apps/oracle/client/11.2.0.1/home1/rdbms/public/ociap.h:10794: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用cx_Oracle连接到Oracle实例并执行一些DDL语句:
db = None
try:
db = cx_Oracle.connect('username', 'password', 'hostname:port/SERVICENAME')
#print(db.version)
except cx_Oracle.DatabaseError as e:
error, = e.args
if error.code == 1017:
print('Please check your credentials.')
# sys.exit()?
else:
print('Database connection error: %s'.format(e))
cursor = db.cursor()
try:
cursor.execute(ddl_statements)
except cx_Oracle.DatabaseError as e:
error, = e.args
if error.code == 955:
print('Table already exists')
if error.code == 1031:
print("Insufficient privileges - are you sure you're using the owner account?")
print(error.code)
print(error.message)
print(error.context)
cursor.close()
db.commit()
db.close()
Run Code Online (Sandbox Code Playgroud)
但是,我不太确定这里的异常处理的最佳设计是什么.
首先,我db在try块中创建对象,以捕获任何连接错误.
但是,如果它无法连接,那么db将不再存在 …
我安装了该库,当尝试使用我的凭据在 jupyter notebook 中访问 SQL 时,出现以下错误:
数据库错误:DPI-1047:找不到 64 位 Oracle 客户端库:“找不到指定的模块”。请参阅 https://oracle.github.io/odpi/doc/installation.html#windows 寻求帮助
我正在尝试为Python 2.6安装cx_Oracle,但它失败了.我对C或MS Vis知之甚少.Studio的编译器甚至可以自己修复它.
这是命令行上的输出:
C:\pydev\cx_Oracle-5.0.1>C:\python26\python setup.py install
running install
running build
running build_ext
building 'cx_Oracle' extension
C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Oracle\instantclient\sdk\include -IC:\p
ython26\include -IC:\python26\PC /Tccx_Oracle.c /Fobuild\temp.win32-2.6-11g\Release\cx_Oracle.obj -DBUILD_VERSION=5.0.1
cx_Oracle.c
c:\pydev\cx_oracle-5.0.1\StringVar.c(392) : warning C4018: '>' : signed/unsigned mismatch
c:\pydev\cx_oracle-5.0.1\StringVar.c(417) : warning C4018: '>' : signed/unsigned mismatch
c:\pydev\cx_oracle-5.0.1\ObjectVar.c(117) : warning C4018: '<' : signed/unsigned mismatch
c:\pydev\cx_oracle-5.0.1\ObjectVar.c(134) : warning C4018: '<' : signed/unsigned mismatch
c:\pydev\cx_oracle-5.0.1\Variable.c(331) : error C2036: 'void *' : unknown size
c:\pydev\cx_oracle-5.0.1\Variable.c(878) : warning …Run Code Online (Sandbox Code Playgroud) 我需要在Linux上安装cx_Oracle for Python 2.5(Linux 2.6.18-371.1.2.el5 i686).我已经安装了Oracle客户端10.2.0.4.
我试过以下:1.cx_Oracle tar.gz从下载http://sourceforge.net/projects/cx-oracle/files/.我不知道哪个列出的版本适用于python 2.5和Oracle客户端10.2.0.4,所以请尝试cx_Oracle-5.1.tar.gz.解压缩tar,转到解压缩文件夹并运行python setup.py install.我收到了错误:
Traceback (most recent call last):
File "setup.py", line 187, in <module>
raise DistutilsSetupError("cannot locate Oracle include files")
distutils.errors.DistutilsSetupError: cannot locate Oracle include files
Run Code Online (Sandbox Code Playgroud)
在.bash_profile中我设置了oracle路径:
export ORACLE_HOME=/usr/oracle/10.2.0.4/client
export PATH=$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
Run Code Online (Sandbox Code Playgroud)
如何解决这样的错误,也许我需要另一个版本的cx_Oracle tar?
下载/解压缩cx-Oracle
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement cx-Oracle
No distributions at all found for cx-Oracle
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我正确的解决方案吗?
更新 建议响应后我得到以下错误:
...
cx_Oracle.c:496: warning: …Run Code Online (Sandbox Code Playgroud) 我一直在使用以下函数来制作一个"更具可读性"(可能)的格式,用于从Oracle获取数据.这是功能:
def rows_to_dict_list(cursor):
"""
Create a list, each item contains a dictionary outlined like so:
{ "col1_name" : col1_data }
Each item in the list is technically one row of data with named columns,
represented as a dictionary object
For example:
list = [
{"col1":1234567, "col2":1234, "col3":123456, "col4":BLAH},
{"col1":7654321, "col2":1234, "col3":123456, "col4":BLAH}
]
"""
# Get all the column names of the query.
# Each column name corresponds to the row index
#
# cursor.description returns a list of tuples, …Run Code Online (Sandbox Code Playgroud) cx-oracle ×10
python ×8
oracle ×7
build ×1
database ×1
linux ×1
python-2.5 ×1
python-2.7 ×1
python-3.x ×1
sql ×1
windows-10 ×1