from:http://space.itpub.net/22664653/viewspace-711728 Python ConnectionOracle Database, you need to use the Cx_oracle package.1 Download Cx_oracleDownload address for this package: When downloading http://cx-oracle.sourceforge.net/, be careful to select the Cx_oracle version that corresponds to the operating system and the Oracle version.2 installation [Root@rac3 python]# RPM-IVH cx_oracle-5.1.1-11g-py24-1.x86_64.rpm preparing ... ########################## ################# [100%] 1:cx_oracle ########################################### [100%]
3 Test: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 () connect Ion.close () test results: 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)
the problems encountered: 1 to perform import cx_oracle for Oracle users[root@rac3 ~]# python python 2.4.3 (#1, the 2009, 01:11:33) [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 Type ' he LP "," Copyright "," credits "or" license "for the 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>>>
If you also encounter Importerror:libclntsh.so.11.1:cannot open shared object file:no such file or directory to view in Oracle. Bash_profile Thingand environment variable Ld_library_path, be sure to set to: ld_library_path= $ORACLE _home/lib:/usr/lib:/usr/local/lib;export ld_library_path 2 Ora-12505:tns:listener does not currently know the SID given in Connect descriptorOracle@rac3:/home/oracle/python>python test.py Traceback (most recent call last): File "test.py", line ten, 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 = (S erver = dedicated) (service_name = yangdb)) Listener.ora file Sid_list_listener = (Sid_list = (Sid_desc = (Sid_name = plsextproc) (Oracle_home =/opt/oracle/11.2.0/alifpre) (progra M = Extproc)) (Sid_desc = (Global_dbname = Yang) (oracle_home =/opt/oracle/11.2.0/ Alifpre) (Sid_name =yangdb))
(Global_dbname = yang) inconsistent with the TNS connection name in the Tnsnams.ora file, modified to Yangdb, restart listening or lsnrclt reload