python36連接oracle

1.查看要連接的oracle版本號

select * from v$version;  

2.下載對應的oracle客戶端,一定要下載對應的客戶端,不然會報錯,64位下載64位的客戶端,oracle版本號也要對應

如上面要連接的版本號爲 Version 12.2.0.1.0 - Production,下載時也應下載Version 12.2.0.1.0

下載地址:https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html

 

3.將下載後的文件解壓,放在Python\Python36\Lib\site-packages,並將Python\Python36\Lib\site-packages\instantclient_12_2加入環境變量

4.安裝cx-Oracle包.地址:http://cx-oracle.sourceforge.net/

   pip install cx-Oracle

5.連接oracle 

import cx_Oracle
'''方法一:用戶名、密碼和監聽寫在一起'''
db = cx_Oracle.connect('username/password@host/orcl')
​
'''方法二:用戶名、密碼和監聽分開寫'''
db = cx_Oracle.connect('username','password','host/orcl')
​
'''方法三:配置監聽並連接'''
tns = cx_Oracle.makedsn('host',1521,'orcl')
db = cx_Oracle.connect('username','password',tns)
#創建光標
mycursor=conn.cursor()

查詢: sql = '''SELECT * FROM TABLE'''

mycursor.execute(sql)  #執行SQL

result= mycursor.fetchall()       # fetchall()    獲取所有記錄,result類型爲list

for i in result:
    print(i)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章