日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Oracle】Python 连接Oracle 数据库

發布時間:2023/12/9 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Oracle】Python 连接Oracle 数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

From:?http://space.itpub.net/22664653/viewspace-711728

Python 連接Oracle 數據庫,需要使用cx_Oracle 包。 1 下載cx_Oracle 該包的下載地址:http://cx-oracle.sourceforge.net/ 下載的時候,注意選擇與操作系統和oracle版本相對應的cx_Oracle版本。 2 安裝 [root@rac3 python]# rpm -ivh cx_Oracle-5.1.1-11g-py24-1.x86_64.rpm? Preparing... ? ? ? ? ? ? ? ?########################################### [100%] ? ?1:cx_Oracle ? ? ? ? ? ? ?########################################### [100%]
3 測試: ?? oracle@rac3:/home/oracle/python>cat test.py? import cx_Oracle? username = "yang"? userpwd = "yang"? host = "127.0.0.1"? port = 1523? dbname = "yangdb"?
dsn=cx_Oracle.makedsn(host, port, dbname)? connection=cx_Oracle.connect(username, userpwd, dsn)? cursor = connection.cursor()? sql = "select * from tab"? cursor.execute(sql)? result = cursor.fetchall()? count = cursor.rowcount? print "====================="? print "Total:", count? print "====================="? for row in result:? ? ? ? ? print row? cursor.close()? connection.close() 測試結果: oracle@rac3:/home/oracle/python>python test.py? ===================== Total: 9 ===================== ('BIG_TABLE', 'TABLE', None) ('BIN$sgD3dAkmWHfgQPoK8Qcq3Q==$0', 'TABLE', None) ('BIND', 'TABLE', None) ('IM_SMS_ADD_FRIEND', 'TABLE', None) ('PARALTAB', 'TABLE', None) ('T1', 'TABLE', None) ('T2', 'TABLE', None) ('T3', 'TABLE', None) ('T4', 'TABLE', None)
遇到的問題: 1 要在oracle 用戶才能執行import cx_Oracle [root@rac3 ~]# python Python 2.4.3 (#1, Jan 21 2009, 01:11:33)? [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cx_Oracle Traceback (most recent call last): ? File "<stdin>", line 1, in ? ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory >>>
如果在oracle 也遇到 ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory 要查看.bash_profile 文件:和環境變量 LD_LIBRARY_PATH有關,一定要設置為: LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:/usr/local/lib;export LD_LIBRARY_PATH 2 ORA-12505: TNS:listener does not currently know of SID given in connect descriptor oracle@rac3:/home/oracle/python>python test.py? Traceback (most recent call last): ? File "test.py", line 10, in ? ? ? connection=cx_Oracle.connect(username, userpwd, dsn)? cx_Oracle.DatabaseError: ORA-12505: TNS:listener does not currently know of SID given in connect descriptor
oracle@rac3:/opt/oracle/11.2.0/alifpre/network/admin>cat tnsnames.ora? # tnsnames.ora Network Configuration File: /opt/oracle/11.2.0/alifpre/network/admin/tnsnames.ora # Generated by Oracle configuration tools.
yangdb = ? (DESCRIPTION = ? ? (ADDRESS = (PROTOCOL = TCP)(HOST = 10.250.7.241 )(PORT = 1523)) ? ? (CONNECT_DATA = ? ? ? (SERVER = DEDICATED) ? ? ? (SERVICE_NAME = yangdb) ? ? ) ? ) listener.ora 文件中 SID_LIST_LISTENER = ? ? ? (SID_LIST = ? ? ? ? ?(SID_DESC = ? ? ? ? ? ?(SID_NAME = PLSExtProc) ? ? ? ? ? ? ?(ORACLE_HOME = /opt/oracle/11.2.0/alifpre) ? ? ? ? ? ?(PROGRAM = extproc) ? ? ? ? ?) ? ? ? ? (SID_DESC = ? ? ? ? ? (GLOBAL_DBNAME = yang) ? ? ? ? ? ?(ORACLE_HOME = /opt/oracle/11.2.0/alifpre) ? ? ? ? ? (SID_NAME =yangdb) ? ? ? ? ?) ? ? ?)
? (GLOBAL_DBNAME = yang)與tnsnams.ora 文件中的tns連接名不一致導致,修改為yangdb,重新啟動監聽或者lsnrclt reload 即可

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【Oracle】Python 连接Oracle 数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。