Python 操作Oralce資料庫
來源:互聯網
上載者:User
串連資料庫的幾種方式:文法:cx_Oracle.connect('username','pwd','IP/HOSTNAME:PORT/TNSNAME')import cx_Oracle
db1=cx_Oracle.connect('yang','yang','127.0.0.1:1523/yangdb')
db2=cx_Oracle.connect('yang/yang@127.0.0.1:1523/yangdb')對於dsn 方式:
makedsn(IP/HOST,PORT,TNSNAME)
dsn=cx_Oracle.makedsn('127.0.0.1','1523','yangdb')db3=cx_Oracle.connect('yang','yang',dsn)例子:oracle@rac3:/home/oracle/python>vim conndb4.py import cx_Oracleusername = "yang"pwd = "yang"
--建立串連dsn=cx_Oracle.makedsn('127.0.0.1','1523','yangdb')db1=cx_Oracle.connect(username,pwd,dsn)
--擷取遊標cursor = db1.cursor()
--執行查詢
sql = "select * from tab" cursor.execute(sql)
--擷取資料 ,可以有多種方式 fetchall(),fetchmang(N)(N 為正整數),fetchone()result = cursor.fetchall() count = cursor.rowcount print "=====================" print "Total:", count print "=====================" for row in result: print row cursor.closedb1.close() oracle@rac3:/home/oracle/python>python conndb4.py =====================Total: 9=====================('BIG_TABLE', 'TABLE', None)('BIN$sgD3dAkmWHfgQPoK8Qcq3Q==$0', 'TABLE', None)('BIND', 'TABLE', None)('IM_SMS_ADD_FRIEND', 'TABLE', None)('PARALTAB', 'TABLE', None)('T1', 'TABLE', None)('T2', 'TABLE', None)('T3', 'TABLE', None)('T4', 'TABLE', None)
1 分析: cx_Oracle.Cursor.parse([statement]) 在執行語句之前對其進行驗證,當此類語句檢測出錯誤的時候,會引發DatabaseError異常
2 執行a cx_Oracle.Cursor.execute(statement,[parameters],**keyword_parameters)此方法可以接受單個參數--一條sql語句直接針對資料庫來運行。通過parameters或keyword_parameters 參數賦值的綁定變數可以指定為字典,學列或者一組關鍵字參數。如果已經提供了字典或關鍵字參數,那麼這些值將與名稱綁定。如果給出的是序列,將根據這些值的位置對他們進行解析。如果是查詢操作,此方法返回一個變數對象列表;如果不是則返回Noneb cx_Oracle.Cursor.executemany(statement,parameter) 對於批量插入尤其有用,因為操作的數量限制為僅僅一個。
3 擷取僅僅適用於查詢--DDL,DCL 語句不返回結果。在不執行查詢的遊標上,這些方法將引發InterfaceError異常3.1
cx_Oracle.Cursor.fetchall()以位元組組列表形式擷取結果集中的所有剩餘行,如果沒有剩餘行。它返回一個空白列表。擷取操作可以通過設定遊標的arraysize 屬性進行調整,該屬性可以設定在每個底層請求中從資料庫中返回的行數。arraysize 的設定越高,需要在網路中往返傳輸的次數越少,arraysize 的預設值為1.3.2
cx_Oracle.Cursor.fetchmany(N)從資料庫中擷取N行。預設為arraysize的值。如果N的值大於擷取到的行的數目,該方法擷取的行數是真實的行數。oracle@rac3:/home/oracle/python>cat conndb2.py import cx_Oracleusername = "yang" pwd = "yang" db1=cx_Oracle.connect(username,pwd,'127.0.0.1:1523/yangdb')cursor = db1.cursor() sql = "select * from tab" cursor.execute(sql) result =
cursor.fetchmany(5) oracle@rac3:/home/oracle/python>python conndb2.py =====================Total:
5=====================('BIG_TABLE', 'TABLE', None)('BIN$sgD3dAkmWHfgQPoK8Qcq3Q==$0', 'TABLE', None)('BIND', 'TABLE', None)('IM_SMS_ADD_FRIEND', 'TABLE', None)('PARALTAB', 'TABLE', None)3.3
cx_Oracle.Cursor.fetchone()擷取一個行資料,單個位元組組。如果無剩餘行則返回none。oracle@rac3:/home/oracle/python>cat conndb2.py import cx_Oracleusername = "yang"pwd = "yang"db1=cx_Oracle.connect(username,pwd,'127.0.0.1:1523/yangdb')cursor = db1.cursor()sql = "select * from tab"cursor.execute(sql)result =
cursor.fetchmany(1) count = cursor.rowcountprint "====================="print "Total:", countprint "====================="for row in result: print rowcursor.closedb1.close()oracle@rac3:/home/oracle/python>python conndb2.py =====================Total: 1=====================('BIG_TABLE', 'TABLE', None)
4 綁定變數模式 cx_Oracle 支援oracle綁定變數的特性bind_val={'dept_id':20,'sal':700}query1=cursor.execute('select * from emp where deptno=:dept_id and salary>:sal',bind_val)query2=cursor.execute('select * from emp where deptno=:dept_id and salary>:sal',bind_val,dept_id=20,sal=700)
可以使用print coursor.bindnames()來查看綁定變數的值。oracle@rac3:/home/oracle/python>python conndb4.py =====================Total: 5=====================(7369, 'SMITH', 'CLERK', 7902, datetime.datetime(1980, 12, 17, 0, 0), 800.0, None, 20)(7566, 'JONES', 'MANAGER', 7839, datetime.datetime(1981, 4, 2, 0, 0), 2975.0, None, 20)(7788, 'SCOTT', 'ANALYST', 7566, datetime.datetime(1987, 4, 19, 0, 0), 3000.0, None, 20)(7876, 'ADAMS', 'CLERK', 7788, datetime.datetime(1987, 5, 23, 0, 0), 1100.0, None, 20)(7902, 'FORD', 'ANALYST', 7566, datetime.datetime(1981, 12, 3, 0, 0), 3000.0, None, 20)
bind_val={'dept_id':20,'sal':700} cursor.execute('select * from emp where deptno=:dept_id and sal>:sal',bind_val)
該過程使用的變數為:
['DEPT_ID', 'SAL']