cx_Oracle连接数据库总结

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

cx_Oracle连接数据库总结
下载好oracle客户端后,在客户端⽬录下新建⼀/network/admin⽬录,并在该⽬录下新建tnsnames.ora⽂件,增加⾃⼰的数据库别名配置。

⽰例如下:
1 MyDB=
2 (DESCRIPTION =
3 (ADDRESS = (PROTOCOL = TCP)(HOST= IP)(PORT = 1521))
4 (CONNECT_DATA =
5 (SERVER = DEDICATED)
6 (SERVICE_NAME = )
7 )
8 )
注意格式要排列好
MyDB为Database名,Host为IP地址, SERVICE_NAME为数据库服务器的实例名。

2.安装的python版本位数也需与操作系统版本位数相同
同时可能出现的其他问题在cx_Oracle官⽅⽂档中也写出了
1. DPI-1047: Oracle Client library cannot be loaded
Check that Python, cx_Oracle and your Oracle Client libraries are all 64-bit or all 32-bit. The DPI-1047 message will tell you whether the 64-bit or 32-bit Oracle Client is needed for your Python.
检查python,cx_Oracle和Oracle Instant Client版本是否⼀致,DPI-1047 message会告诉你安装的客户端版本是否正确。

2.On Windows, restart your command prompt and use set PATH to check the environment variable has the correct Oracle Client listed before any other Oracle directories.
记得配置Oracle客户端的环境变量,例如我的配置是 PATH: E:\oracle\instantclient_12_2;
3.On Windows, use the DIR command on the directory set in PATH. Verify that OCI.DLL exists there.
检查oracle客户端(instantclient)⽬录下存在oci.dll⽂件
On Linux, check the LD_LIBRARY_PATH environment variable contains the Oracle Client library directory.
On macOS, make sure Oracle Instant Client is in ~/lib or /usr/local/lib and that you are not using the bundled Python (use Homebrew or instead).
最后⼀切就绪,程序未出错,但并⽆输出时感觉内⼼有些崩溃
1import cx_Oracle
2
3 connection = cx_Oracle.Connection("Username/密码@Host:Port/SERVICE_NAME")
4 cursor = connection.cursor()
5
6try:
7 cursor.execute("select 1 / 0 from dual")
8except cx_Oracle.DatabaseError as exc:
9 error, = exc.args
10print("Oracle-Error-Code:", error.code)
11print("Oracle-Error-Message:", error.message)
最后查看意识到是执⾏了sql语句,但并未进⾏输出

cursor.execute("select 1 / 0 from dual")
下⼀⾏增加
print(cursor.description)
便可以看见查找到的数据库中的内容。

相关文档
最新文档