Python連線oracle資料庫

勿在浮沙築高臺LS發表於2017-02-20
import cx_Oracle
#連線資料庫
conn = cx_Oracle.connect('賬號/密碼@地址/資料庫名字')  
#建立宣告
cursor = conn.cursor()
#執行查詢
cursor.execute ("select * from ck10_cfmx where rownum<=10")
#獲取結果
row = cursor.fetchone()
for a in row:
    print(a)

實驗結果:

7512042
130177_1
130177
1
C
化驗費
1.0
項
5.0
5.0
5.0
2011-07-18 17:28:00
2011-07-19 08:20:39
250303002
血清甘油三酯測定
14119
None

fetchone()函式返回一條結果。

import cx_Oracle
#連線資料庫
conn = cx_Oracle.connect('賬號/密碼@地址/資料庫名')  
#建立宣告
cursor = conn.cursor()
#執行查詢
cursor.execute ("select * from ck10_cfmx where rownum<=10")
#獲取結果
row = cursor.fetchone()
# for a in row:
#     print(a)
for row in cursor.fetchall():
    print(row)

實驗結果:

(7512043, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 9.0, 9.0, 9.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 19, 8, 20, 39), '250303004', '血清高密度脂蛋白膽固醇測定', 14121, None)
(7512044, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 9.0, 9.0, 9.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 19, 8, 20, 39), '250303005', '血清低密度脂蛋白膽固醇測定', 14123, None)
(7512045, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 5.0, 5.0, 5.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 18, 18, 37, 25), '250304001', '鉀測定', 14172, None)
(7512046, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 5.0, 5.0, 5.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 18, 18, 37, 25), '250304002', '鈉測定', 14173, None)
(7512047, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 5.0, 5.0, 5.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 18, 18, 37, 25), '250304003', '氯測定', 14174, None)
(7512048, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 5.0, 5.0, 5.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 18, 18, 37, 25), '250304004', '鈣測定', 14175, None)
(7512049, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 7.0, 7.0, 7.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 18, 18, 37, 25), '250304010', '血清碳酸氫鹽(HCO3) 測定', 14182, None)
(7512050, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 5.0, 5.0, 5.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 19, 8, 20, 39), '250305001', '血清總膽紅素測定', 14185, None)
(7512051, '130177_1', 130177, 1, 'C', '化驗費', 1.0, '項', 5.0, 5.0, 5.0, datetime.datetime(2011, 7, 18, 17, 28), datetime.datetime(2011, 7, 19, 8, 20, 39), '250305002', '血清直接膽紅素測定', 14186, None)

fetchall函式返回所有結果。

相關文章