cx_Oracle连接数据库总结
下载好oracle客户端后,在客户端⽬录下新建⼀/network/admin⽬录,并在该⽬录下新建a⽂件,增加⾃⼰的数据库别名配置。⽰例如下:
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 instead).
最后⼀切就绪,程序未出错,但并⽆输出时感觉内⼼有些崩溃
1import cx_Oracle
2
3 connection = cx_Oracle.Connection("Username/密码@Host:Port/SERVICE_NAME")
4 cursor = connection.cursor()
5
6try:
7  ute("select 1 / 0 from dual")
linux系统安装oracle11g8except cx_Oracle.DatabaseError as exc:
9  error, = exc.args
10print("Oracle-Error-Code:", de)
11print("Oracle-Error-Message:", ssage)
最后查看意识到是执⾏了sql语句,但并未进⾏输出
下⼀⾏增加
print(cursor.description)
便可以看见查到的数据库中的内容

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。