轉(zhuǎn):https://www.cnblogs.com/cyxiaer/p/9396861.html
?
必需的Oracle鏈接庫(kù)的下載地址:https://www.oracle.com/technetwork/topics/winx64soft-089540.html
只連接數(shù)據(jù)庫(kù)的話不必安裝客戶(hù)端:
1. 把cx_Oracle的客戶(hù)端文件復(fù)制到site-packages/ 目錄下,可能是Python, Anaconda, venv下面的安裝包里
2. 把下載的instantclient文件夾下的.dll文件全部復(fù)制到site-packages/ 目錄下
3. 把instantclient文件解壓后的地址添加到環(huán)境變量里面去。
4. 創(chuàng)建數(shù)據(jù)庫(kù)連接.
創(chuàng)建數(shù)據(jù)庫(kù)連接connect和關(guān)閉數(shù)據(jù)庫(kù)連接close
創(chuàng)建數(shù)據(jù)庫(kù)連接的三種方式:
方法一:用戶(hù)名、密碼和監(jiān)聽(tīng)分開(kāi)寫(xiě)
import cx_Oracle
db=cx_Oracle.connect('username/password@host/orcl')
db.close()
?
方法二:用戶(hù)名、密碼和監(jiān)聽(tīng)寫(xiě)在一起
import cx_Oracle
db=cx_Oracle.connect('username','password','host/orcl')
db.close()
?
方法三:配置監(jiān)聽(tīng)并連接
import cx_Oracle
tns=cx_Oracle.makedsn('host',1521,'orcl')
db=cx_Oracle.connect('username','password',tns)
db.close()
?
cx_Oracle錯(cuò)誤:Unable?to?acquire?Oracle?environment?handle
錯(cuò)誤表現(xiàn):
cx_Oracle連接Oracle數(shù)據(jù)庫(kù)的時(shí)候報(bào)錯(cuò):
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
解決辦法:將instantclient目錄下的所有*.dll文件拷貝到Python27\Lib\site-packages目錄下,問(wèn)題解決
SQLAlchemy Oracle 的中文問(wèn)題
你需要設(shè)置?
NLS_LANG
?環(huán)境變量,否則你讀取出來(lái)的中文可能是亂碼,或者當(dāng) insert 的數(shù)據(jù)有中文時(shí)會(huì)導(dǎo)致 Unicode 編碼錯(cuò)誤。
你可以在 Python 代碼中這么設(shè)置環(huán)境變量
# 設(shè)置編碼,否則:
# 1. Oracle 查詢(xún)出來(lái)的中文是亂碼
# 2. 插入數(shù)據(jù)時(shí)有中文,會(huì)導(dǎo)致
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-7: ordinal not in range(128) os.environ[
'NLS_LANG'] =
'SIMPLIFIED CHINESE_CHINA.UTF8'
No
module named cx_Oracle:
import cx_Oracle
ImportError:
No
module named cx_Oracle
如果安裝的 python 64 位,需要把cx_Oracle文件復(fù)制到 /usr/lib64/python2.7/site-packages/ 目錄下
cd
/usr/lib
/python2.7/site-packages/
cp cx_Oracle.so
/usr/lib64
/python2.7/site-packages/cx_Oracle.so cp cx_Oracle
-5.1
.2-py2
.7.egg-info
/usr/lib64
/python2.7/site-packages/cx_Oracle
-5.1
.2-py2
.7.egg-info
UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position ... 問(wèn)題解決辦法
目標(biāo)文件的編碼是導(dǎo)致標(biāo)題所指問(wèn)題的罪魁禍?zhǔn)?
f = open(
"out.html",
"w")
,在windows下面,新文件的默認(rèn)編碼是gbk,這樣的話,python解釋器會(huì)用gbk編碼去解析我們的網(wǎng)絡(luò)數(shù)據(jù)流txt,然而txt此時(shí)已經(jīng)是decode過(guò)的unicode編碼,這樣的話就會(huì)導(dǎo)致解析不了,出現(xiàn)上述問(wèn)題。 解決的辦法就是,改變目標(biāo)文件的編碼:
f = open(
"out.html",
"w",encoding=
'utf-8')
問(wèn)題解決。
1. cx_Oracle
cx_Oracle模塊是Python連接Oracle數(shù)據(jù)庫(kù)的模塊,在Python中,如果要連接Oracle,必須先安裝cx_Oracle模塊。?
cx_Oracle的下載地址:https://pypi.python.org/pypi/cx_Oracle/?
選擇和操作系統(tǒng)、Python版本一致的安裝包進(jìn)行安裝。當(dāng)然為了省事兒,你也可以直接使用pip命令來(lái)安裝cx_Oracle。
pip install cx_Oracle
安裝完成后,在交互模式下輸入import cx_Oracle,不報(bào)錯(cuò),說(shuō)明安裝成功。
2. 連接Oracle數(shù)據(jù)庫(kù)
方法一:用戶(hù)名、密碼、監(jiān)聽(tīng)分開(kāi)寫(xiě)
import cx_Oracle
db=cx_Oracle.connect('username','password','host:port/sid')
方法二:用戶(hù)名、密碼、監(jiān)聽(tīng)寫(xiě)一起
import cx_Oracle?
db=cx_Oracle.connect('username/password@host:port/sid')?
方法三:先配置監(jiān)聽(tīng),后連接
import cx_Oracle?
tnsname = cx_Oracle.makedsn('host', port,'sid')?
db = cx_Oracle.connect('username','password',tnsname)?
說(shuō)明:代碼中username、password、host、port、sid換成實(shí)際數(shù)據(jù)庫(kù)的用戶(hù)名、密碼、主機(jī)名或主機(jī)IP、數(shù)據(jù)庫(kù)實(shí)例名。
3. 創(chuàng)建游標(biāo)
cx_Oracle中,對(duì)于數(shù)據(jù)庫(kù)的增刪改查操作需要通過(guò)游標(biāo)來(lái)進(jìn)行,游標(biāo)的創(chuàng)建語(yǔ)句如下:
cur=db.cursor()
4. 執(zhí)行sql語(yǔ)句
Sql語(yǔ)句書(shū)寫(xiě):不需要從外部傳入?yún)?shù),可以直接書(shū)寫(xiě)sql語(yǔ)句,然后使用execute執(zhí)行sql即可;如果需要從外部傳入?yún)?shù),在需要傳入?yún)?shù)的地方使用變量,并在變量前加“:”,然后通過(guò)prepare加載sql語(yǔ)句。
cur.prepare:如果執(zhí)行的sql語(yǔ)句需要傳外部參數(shù),可以先用這個(gè)函數(shù)加載sql語(yǔ)句,然后再通過(guò)execute或executemany加參數(shù)執(zhí)行。
cur.execute:執(zhí)行單條sql語(yǔ)句。
cur.executemany:執(zhí)行多條sql語(yǔ)句。
關(guān)于execute需要說(shuō)明的是如果執(zhí)行的sql語(yǔ)句不需要從外部傳入?yún)?shù),那么可以跳過(guò)prepare,直接將sql語(yǔ)句作為execute的第一個(gè)參數(shù)來(lái)執(zhí)行sql。
db.commit():執(zhí)行提交操作,增、刪、改后需要使用。
cur.fetchall:在查詢(xún)之后使用,獲取所有查詢(xún)到的結(jié)果記錄。
cur.fetchone:在查詢(xún)之后使用,獲取一條查詢(xún)到的結(jié)果記錄。
關(guān)于fetchall和fetchone需要說(shuō)明的是查詢(xún)到的記錄一旦被提取,就不能再次被提取,不管是用fetchall提取還是使用fetchone提取。
res?
=
?
cur.fetchall()[
0
][
0
].read();
fetchall和fetchone返回的是元組,加上[][],可以直接取到值。
查詢(xún):?
需要外部參數(shù):
>>> cur.prepare('select * from t_emp a where a.empid=:id')
>>> cur.execute(None,{'id':id})
>>> cur.fetchall()
不需要外部參數(shù):
>>> cur.execute("select e.empid,e.empname from t_emp e")
>>> cur.fetchone()
(1, '張三')
>>> cur.fetchall()
[(2, '李四'), (3, '王五'), (4, '沈六'), (5, '田七'), (6, '鳳九')]
增加、刪除、修改:
單條增加:
>>> sql="insert into t_emp(empid,empname) values (:empid,:empname)"
>>> cur.prepare(sql)
>>> cur.execute(None,{'empname':'李紳','empid':7})
>>> db.commit()
多條增加:
>>> sql="insert into t_emp(empid,empname) values (:empid,:empname)"
>>> cur.prepare(sql)
>>> cur.executemany(None,[{'empname':'趙青','empid':8},{'empname':'蕭遠(yuǎn)','empid':9}])
>>> db.commit()
單條修改:
>>> sql="update t_emp a set a.empname='清月' where a.empid=:empid"
>>> cur.prepare(sql)
>>> cur.execute(None,{empid:"4"})
>>> db.commit()
多條修改:
>>> sql="update t_emp a set a.empname=:empnamewhere a.empid=:empid"
>>> cur.prepare(sql)
>>> cur.executemany(None,[{'empid':"5","empname":"明月"},{'empid':"6","empname":"樂(lè)天"}])
>>> db.commit()
刪除:
>>> cur.execute('delete from t_emp a where a.empid in (3,4,5,6)')
>>> db.commit()
5. 關(guān)閉游標(biāo)
sql語(yǔ)句執(zhí)行結(jié)束,不再使用時(shí),應(yīng)關(guān)閉游標(biāo),關(guān)閉游標(biāo)的語(yǔ)句為:
cur.close()
6. 關(guān)閉數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)操作結(jié)束后應(yīng)及時(shí)釋放連接,關(guān)閉數(shù)據(jù)庫(kù)連接的語(yǔ)句為:
db.close()
7. 我寫(xiě)的一個(gè)Oracle數(shù)據(jù)庫(kù)操作類(lèi)
cx_Oracle是Python的Oracle操作的模塊,在使用時(shí)導(dǎo)入就能使用,但是因?yàn)閿?shù)據(jù)庫(kù)使用時(shí)涉及連接、操作、提交、關(guān)閉連接等一系列操作,不可能每次使用時(shí)都把這些操作用代碼寫(xiě)一遍,所以我把這些操作放到一個(gè)類(lèi)里,在實(shí)際使用時(shí)來(lái)調(diào)用這個(gè)類(lèi)就行了。
#coding=utf-8 import cx_Oracle class OpOracle(): def __init__(self,ora_username,ora_password,ora_host,ora_port,ora_sid): '''初始化Oracle連接''' self.db=cx_Oracle.connect(ora_username,ora_password,ora_host+':'+ora_port+'/'+ora_sid) self.cur=self.db.cursor() def Ora_Select(self,strSql): '''執(zhí)行strSql語(yǔ)句進(jìn)行查詢(xún)''' self.cur.execute(strSql) return self.cur.fetchall() def Ora_IUD_Single(self,strSql): '''執(zhí)行strSql語(yǔ)句進(jìn)行增加、刪除、修改操作''' self.cur.execute(strSql) self.db.commit() def Ora_IUD_Multi(self,strSql,List): '''執(zhí)行strSql語(yǔ)句進(jìn)行增加、刪除、修改操作,對(duì)應(yīng)參數(shù)使用List中的數(shù)據(jù)''' self.cur.prepare(strSql) self.cur.executemany(None,List) self.db.commit() def Ora_Cur_Close(self): '''關(guān)閉游標(biāo)''' self.cur.close() def Ora_db_Close(self): '''關(guān)閉Oracle數(shù)據(jù)庫(kù)連接''' self.db.close()
?
我把這段代碼保存在OpOracle.py文件中,使用時(shí)直接導(dǎo)入這個(gè)文件即可。如:
from OpOracle import OpOracle ora=OpOracle('cs','ceshi','192.168.1.226','1521','db_emp') l_emp=ora.Ora_Select('select * from t_emp') #查詢(xún)t_emp表的數(shù)據(jù)并保存到l_emp列表中 ora.Ora_IUD_Single('delete from t_emp a where a.empid=1') #刪除empid為1的記錄 ora.Ora_Cur_Close() ora.Ora_db_Close() #最后記得關(guān)閉游標(biāo)和數(shù)據(jù)庫(kù)連接
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
