python連接oracle數(shù)據(jù)庫(kù)相對(duì)較為麻煩,需要先配置客戶端的環(huán)境
下面介紹window和ubuntu配置方式
相關(guān)軟件包的下載鏈接
https://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
http://www.liangchan.net/soft/softdown.asp?softid=3106
Ubuntu環(huán)境配置
1、安裝cx_oracle模塊,但是這個(gè)模塊還是不能用,需要往下配置一些東西
pip3 install cx_Oracle
2、下載所需的軟件包:
注意:根據(jù)自己連接oracle數(shù)據(jù)庫(kù)版本下載

3、在/usr/local/目錄下創(chuàng)建Oracle文件夾
sudo mkdir /usr/local/Oracle
4、解壓instantclient-basic-linux.x64-11.2.0.4.0.zip和instantclient-sdk-linux.x64-11.2.0.4.0.zip,如圖:
注意:兩個(gè)文件夾下的文件都放在Oracle目錄下,這步很重要!很重要!很重要!

5、配置環(huán)境變量
sudo vim /etc/profife
將下面復(fù)制到profife文件末尾
export ORACLE_HOME=/usr/local/Oracle
export PATH=$PATH:$ORACLE_HOME
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
# 下面兩行是解決編碼問(wèn)題,如果不設(shè)置下面兩行,從oracle導(dǎo)出中文數(shù)據(jù)會(huì)出現(xiàn)亂碼
export LANG="zh_CN.UTF-8"
export NLS_LANG=AMERICAN_AMERICA.UTF8
6、重啟Ubuntu系統(tǒng)
reboot
Window系統(tǒng)配置
1、安裝cx_oracle模塊,還是跟Ubuntu一樣,這樣安裝之后不能用,需要配置環(huán)境
pip3 install cx_Oracle
2、下載所需的軟件包
注意:根據(jù)自己連接oracle數(shù)據(jù)庫(kù)版本和系統(tǒng)位數(shù)下載

3、解壓instantclient-basic-windows.x64-11.2.0.4.0.zip,并將整個(gè)文件移動(dòng)到某盤(pán)位置,本人是放置到C:\Program Files,然后點(diǎn)擊運(yùn)行instantclient_11_2里的adrci.exe如圖:

4、雙擊安裝vcredist_x64.exe
5、配置path變量
1) 將instantclient_11_2路徑添加到path環(huán)境變量中;如果本地還安裝有oracle數(shù)據(jù)庫(kù),要添加到oracle的路徑前面

2)新建ORACLE_HOME,TNS_ADMIN,NLS_LANG 三個(gè)變量,NLS_LANG是為了防止中文亂碼



6、將instantclient_11_2目錄下oci.dll,oraocci11.dll,oraociei11.dll復(fù)制到python\Lib\site-packages目錄下
7、創(chuàng)建一個(gè)監(jiān)聽(tīng)文件tnsnames.ora到C:\Program Files\instantclient_11_2,打開(kāi)添加以下內(nèi)容,并保存
oral=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST= IP地址)(PORT =端口))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME =數(shù)據(jù)庫(kù)名稱)
) )
cx_Oracle模塊
使用cx_Oracle模塊連接有三種方式:
第一種方式
cx_Oracle.connect('賬號(hào)/密碼@ip:端口/數(shù)據(jù)庫(kù)名稱')
第二種方式
cx_Oracle.connect('賬號(hào)', '密碼', 'ip:端口/數(shù)據(jù)庫(kù)名稱')
第三種方式
tns = cx_Oracle.makedsn('ip', '端口', '數(shù)據(jù)庫(kù)名稱')
cx_Oracle.connect('賬號(hào)', '密碼', tns)
python操作oracle數(shù)據(jù)庫(kù)方式
import cx_Oracle
class PyOracle(object):
"""創(chuàng)建python操作oracle類"""
def __init__(self, username, password, hosts):
# 連接oracle數(shù)據(jù)庫(kù)
self.conn = cx_Oracle.connect(username, password, hosts)
# 建立游標(biāo)對(duì)象
self.cursor = self.conn.cursor()
def __del__(self):
# 關(guān)閉數(shù)據(jù)庫(kù)連接
self.cursor.close()
self.conn.close()
def get_row(self, sql):
# 生成器獲取數(shù)據(jù)
self.cursor.execute(sql)
for row in self.cursor:
yield row
def get_column(self, sql):
# 獲取字段
self.cursor.execute(sql)
fieldnames = []
columns = self.cursor.description
if columns:
for column in columns:
column = column[0].lower()
fieldnames.append(column)
return fieldnames
def get_one(self, sql):
# 獲取一條數(shù)據(jù)
self.cursor.execute(sql)
return self.cursor.fetchone()
def get_all(self, sql):
# 獲取所有數(shù)據(jù)
self.cursor.execute(sql)
return self.cursor.fetchall()
參考資料:
cx_oracle官方文檔:https://cx-oracle.readthedocs.io/en/latest/module.html