您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關數據庫中cx_oracle怎么用使用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
連接
import cx_Oracle #使用tnsnames文件別名鏈接 # ora = cx_Oracle.connect('scott/tiger@orcl') #使用字符串,傳入一個參數鏈接 # ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl') # 使用字符串,分別傳入用戶名密碼等 # ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl') # 使用dsn解析成tns字符串,連接數據庫 # tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl') # ora = cx_Oracle.connect('scott','tiger',tnsname) #使用sysdba或者其他角色鏈接 ora = cx_Oracle.connect('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA) cursor = ora.cursor() #使用位置對應參數 cursor.execute('select * from scott.t1 where DEPTNO = :1',(10,)) print(cursor.fetchall()) cursor.close() ora.close() |
查詢
#fetchall import cx_Oracle ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl') cursor = ora.cursor() cursor.execute('select * from emp') print(cursor.fetchall()) cursor.close() ora.close() |
#fetchone import cx_Oracle ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl') cursor = ora.cursor() cursor.execute('select * from emp') while 1: res = cursor.fetchone() if res == None: break print(res) cursor.close() ora.close() |
#fetchmany # 使用dsn解析成tns字符串,連接數據庫 tnsname = cx_Oracle.makedsn('192.168.56.151','1521','orcl') ora = cx_Oracle.connect('system','oracle',tnsname) cursor = ora.cursor() cursor.execute('select * from dba_objects') resCount=0 while 1: res = cursor.fetchmany(10) if res == []: break print(res) resCount += 10 cursor.close() ora.close() |
#使用綁定變量 import cx_Oracle ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl') cursor = ora.cursor() #使用位置對應參數 cursor.execute('select * from t1 where DEPTNO = :1',(10,)) print(cursor.fetchall()) #使用字典傳入參數 param={'dno':20} cursor.execute('select * from t1 where DEPTNO = :dno',param) print(cursor.fetchall()) cursor.execute('select * from t1 where DEPTNO = :dno or DNAME=:dn',dno=40,dn='ACCOUNTING') print(cursor.fetchall()) cursor.close() ora.close() |
增、刪、改 數據和多次執行
import cx_Oracle #使用tnsnames文件別名鏈接 # ora = cx_Oracle.connect('scott/tiger@orcl') #使用字符串,傳入一個參數鏈接 # ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl') # 使用字符串,分別傳入用戶名密碼等 # ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl') # 使用dsn解析成tns字符串,連接數據庫 tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl') ora = cx_Oracle.connect('scott','tiger',tnsname) #使用sysdba或者其他角色鏈接 # ora = cx_Oracle.connect('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA) cursor = ora.cursor() cursor.execute('insert into t1 values(50,:1,:2)',('DBA','CHINA')) #sql中使用參數 ora.commit() cursor.execute('select * from t1') while 1: res = cursor.fetchone() if res == None: break print(res) cursor.close() ora.close() |
import cx_Oracle #使用tnsnames文件別名鏈接 # ora = cx_Oracle.connect('scott/tiger@orcl') #使用字符串,傳入一個參數鏈接 # ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl') # 使用字符串,分別傳入用戶名密碼等 # ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl') # 使用dsn解析成tns字符串,連接數據庫 tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl') ora = cx_Oracle.connect('scott','tiger',tnsname) #使用sysdba或者其他角色鏈接 # ora = cx_Oracle.connect('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA) cursor = ora.cursor() cursor.prepare('update t1 set LOC=:loc where DEPTNO=:dno') cursor.execute(None,{'loc':'BEIJING','dno':50}) #使用了prepare函數,在execute里面可以不傳入sql語句,直接傳入參數。注意:這里的第一個參數必須為None ora.commit() cursor.execute('select * from t1') while 1: res = cursor.fetchone() if res == None: break print(res) cursor.close() ora.close() |
import cx_Oracle #使用tnsnames文件別名鏈接 # ora = cx_Oracle.connect('scott/tiger@orcl') #使用字符串,傳入一個參數鏈接 # ora = cx_Oracle.connect('scott/tiger@192.168.56.152:1521/orcl') # 使用字符串,分別傳入用戶名密碼等 # ora = cx_Oracle.connect('scott','tiger','192.168.56.152:1521/orcl') # 使用dsn解析成tns字符串,連接數據庫 tnsname = cx_Oracle.makedsn('192.168.56.152','1521','orcl') ora = cx_Oracle.connect('scott','tiger',tnsname) #使用sysdba或者其他角色鏈接 # ora = cx_Oracle.connect('sys','oracle','192.168.56.152:1521/orcl',mode=cx_Oracle.SYSDBA) cursor = ora.cursor() #執行多條語句 list1 = [(60,'Enginer','Sydney'),(70,'Diver','South Africa')] cursor.prepare('insert into t1 values(:1,:2,:3)') cursor.executemany(None,list1) #使用了prepare函數,在execute里面可以不傳入sql語句,直接傳入參數。注意:這里的第一個參數必須為None ora.commit() cursor.execute('select * from t1') while 1: res = cursor.fetchone() if res == None: break print(res) cursor.close() ora.close() |
調用函數和存儲過程
#調用存儲過程 cursor.callproc(name, parameters=[], keywordParameters={}) |
#調用函數 cursor.callfunc(name, returnType, parameters=[], keywordParameters={}) #cx_Oracle.STRING |
cx_Oracle、Python的對象類型之間存在轉換關系
Oracle | cx_Oracle | Python |
VARCHAR2, NVARCHAR2, LONG | cx_Oracle.STRING | str |
CHAR | cx_Oracle.FIXED_CHAR | str |
NUMBER | cx_Oracle.NUMBER | int |
FLOAT | cx_Oracle.NUMBER | float |
DATE | cx_Oracle.DATETIME | datetime.datetime |
TIMESTAMP | cx_Oracle.TIMESTAMP | datetime.datetime |
CLOB | cx_Oracle.CLOB | cx_Oracle.LOB |
BLOB | cx_Oracle.BLOB | cx_Oracle.LOB |
獲取中文亂碼
import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8’ #或者os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8' |
感謝各位的閱讀!關于“數據庫中cx_oracle怎么用使用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。