日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

【转】python 和 cx_Oracle 的使用

發(fā)布時(shí)間:2025/3/20 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】python 和 cx_Oracle 的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下載地址:

http://sourceforge.net/projects/cx-oracle/?source=directory(失效的話請(qǐng)自己百度)下載的時(shí)候注意數(shù)據(jù)庫版本和操作系統(tǒng)環(huán)境

技術(shù)手冊(cè):

http://starship.python.net/crew/atuining/cx_Oracle/html/cx_Oracle.html

http://cx-oracle.readthedocs.org/en/latest/index.html

cx_Oracle使用方法

自:http://www.x5dj.com/Blog/00182202/00572497.shtml

正確安裝好cx_oracle之后,要使用它來連接到oracle數(shù)據(jù)庫進(jìn)行操作,具體應(yīng)該分3步走:

第一步:導(dǎo)入cx_Oracle?,建立連接

>>> import cx_Oracle # 導(dǎo)入模塊 >>> db = cx_Oracle.connect('hr', 'hrpwd', 'localhost:1521/XE') #建立連接,3 個(gè)參數(shù)分開寫 >>> db1 = cx_Oracle.connect('hr/hrpwd@localhost:1521/XE') #建立連接,3 個(gè)參數(shù)連寫 >>> dsn_tns = cx_Oracle.makedsn('localhost', 1521, 'XE') >>> print dsn_tns (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SID=XE))) >>> db2 = cx_Oracle.connect('hr', 'hrpwd', dsn_tns) >>> print db.version 10.2.0.1.0 >>> versioning = db.version.split('.') >>> print versioning ['10', '2', '0', '1', '0'] >>> if versioning[0]=='10': ... print "Running 10g" ... elif versioning[0]=='9': ... print "Running 9i" ... Running 10g >>> print db.dsn localhost:1521/XE

第二步:建立?Cursor?光標(biāo)

>>>cursor = db.cursor()
#建立一個(gè)cursor之后,我們可以調(diào)用這個(gè)cursor.execute(‘SQL‘) 來執(zhí)行SQL語句。比如:
>>>cursor.execute(‘select * from tabs’) #執(zhí)行完畢以后,可以調(diào)用cursor.fetchall()一次取完所有結(jié)果,或者cursor.fetchone()一次取一行結(jié)果 >>> row=cursor.fetchall() >>> for x in row:For y in x:Print y,Print 這樣就可以按照表格的形式打印取得的結(jié)果了! 在從oracle取出數(shù)據(jù)的時(shí)候,考慮到它的數(shù)據(jù)類型了嗎?下面就是數(shù)據(jù)類型的對(duì)應(yīng)表

Datatypes

During the fetch stage, basic Oracle data types get mapped into their Python equivalents. cx_Oracle maintains a separate set of data types that helps in this transition. The Oracle - cx_Oracle - Python mappings are:
Oraclecx_OraclePython
VARCHAR2
NVARCHAR2
LONG
cx_Oracle.STRINGstr
CHARcx_Oracle.FIXED_CHAR
NUMBERcx_Oracle.NUMBERint
FLOATfloat
DATEcx_Oracle.DATETIMEdatetime.datetime
TIMESTAMPcx_Oracle.TIMESTAMP
CLOBcx_Oracle.CLOB cx_Oracle.LOB
BLOBcx_Oracle.BLOB
帶參數(shù)的查詢:
>>> named_params = {'dept_id':50, 'sal':1000} >>> query1 = cursor.execute('SELECT * FROM employees WHERE department_id=:dept_id AND salary>:sal', named_params) >>> query2 = cursor.execute('SELECT * FROM employees WHERE department_id=:dept_id AND salary>:sal', dept_id=50, sal=1000)

這種是名字參數(shù),還可以按位置參數(shù):

r1 = cursor.execute('SELECT * FROM locations WHERE country_id=:1 AND city=:2', ('US', 'Seattle')) 注意: 當(dāng)只有一次參數(shù)的時(shí)候,也要把它寫成元組的形式,比如 Cursor.execute(‘select name from user where id=:1’,(login_Id,))

千萬要注意,login_id后面還帶有一個(gè)逗號(hào),如果沒有逗號(hào),他其實(shí)就是一個(gè)數(shù)據(jù)對(duì)象,但是當(dāng)他后面有個(gè)逗號(hào)的時(shí)候,他就變成了元組的一個(gè)數(shù)據(jù)項(xiàng),千萬要記住啊,我就是在這里徘徊了很久。!

Cursor. Prepare的用法, 這個(gè)方法就是在prepare之后,你再去execute的時(shí)候,就不用寫上sql語句參數(shù)了 >>> cursor.prepare('SELECT * FROM jobs WHERE min_salary>:min') >>> r = cursor.execute(None, {'min':1000}) #注意,第一個(gè)參數(shù)是None 一次執(zhí)行多條sql語句: Large insert operations don't require many separate inserts because Python fully supports inserting many rows at once with the cx_Oracle.Cursor.executemany method. Limiting the number of execute operations improves program performance a lot and should be the first thing to think about when writing applications heavy on INSERTs. Let's create a table for a Python module list, this time directly from Python. You will drop it later. >>> create_table = """ CREATE TABLE python_modules ( module_name VARCHAR2(50) NOT NULL, file_path VARCHAR2(300) NOT NULL ) """ >>> from sys import modules >>> cursor.execute(create_table) >>> M = [] >>> for m_name, m_info in modules.items(): ... try: ... M.append((m_name, m_info.__file__)) ... except AttributeError: ... pass ... >>> len(M) 76 >>> cursor.prepare("INSERT INTO python_modules(module_name, file_path) VALUES (:1, :2)") >>> cursor.executemany(None, M) >>> db.commit() >>> r = cursor.execute("SELECT COUNT(*) FROM python_modules") >>> print cursor.fetchone() (76,) >>> cursor.execute("DROP TABLE python_modules PURGE")

?BLOB & CLOB 格式的創(chuàng)建:

binary_content = cursor.var(cx_Oracle.BLOB)
binary_content.setvalue(0, content)

轉(zhuǎn)載于:https://www.cnblogs.com/zijin/archive/2013/03/25/2980906.html

總結(jié)

以上是生活随笔為你收集整理的【转】python 和 cx_Oracle 的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。