python連接oracle
1.OCI安裝
官網(wǎng)地址:
https://www.oracle.com/database/technologies/instant-client/macos-intel-x86-downloads.html
包地址:"Basic Light Package"
https://download.oracle.com/otn_software/mac/instantclient/193000/instantclient-basiclite-macos.x64-19.3.0.0.0dbru.zip
安裝命令與官網(wǎng)一樣:
cd ~
unzip instantclient-basiclite-macos.x64-19.3.0.0.0dbru.zip
mkdir ~/lib
ln -s ~/instantclient_19_3/libclntsh.dylib ~/lib/
2.安裝cx_oracle
https://www.cnblogs.com/songhouhou/p/11106367.html
$ pip3 install cx_oracle
例1:
import cx_Oracle
conn = cx_Oracle.connect('user', 'password', '192.168.3.150:1521/orcl')
cursor = conn.cursor()
print("連接成功!")
cursor.close()
conn.commit()
conn.close()
例2:
# -*- coding: utf-8 -*-
importcx_Oracle
import os
os.environ['NLS_LANG'] ='SIMPLIFIED CHINESE_CHINA.UTF8'
# 設(shè)置編碼,不然select出來的數(shù)據(jù)如果有中文會提示gbk無法轉(zhuǎn)碼
conn = cx_Oracle.connect("username/password@localhost/sid")
# 獲取sid 方法,打開連接了oracle 的客戶端,執(zhí)行 selectinstance_namefromv$instance; 即所得
cursor = conn.cursor ()
cursor.execute ("查詢語句")
row = cursor.fetchmany(numRows=3)
print(row)
cursor.close()
conn.close()
** 測試結(jié)果完美 、2020-03-20、by: muye **