安裝模塊
使用第三方模塊 `cx_Oracle`
pip install cx_Oracle
連接方式
import cx_Oracle as cx #第一種 con = cx.connect('root', 'root123', '127.0.0.1:1521/orcl') #第二種 con = cx.connect('root/root123@127.0.0.1:1521/orcl') #第三種 dsn = cx.makedsn('127.0.0.1', '1521', 'orcl') connection = cx.connect('root', 'root123', dsn)
簡單使用
# -*- coding: utf-8 -*- import cx_Oracle as cx #導入模塊 con = cx.connect('root', 'root123', '127.0.0.1:1521/ORCL') #創建連接 cursor = con.cursor() #創建游標 cursor.execute("select * from TDER where ID='28'") #執行sql語句 data = cursor.fetchone() #獲取一條數據 print(data) #打印數據 cursor.close() #關閉游標 con.close() #關閉數據庫連接
oracle數據庫的操作和mysql的pymysql操作基本相同,可以參考pymysql使用
windows下使用注意
windows下是使用需要安裝`Instant Client`
1) 下載 Instant Client
地址:http://www.oracle.com/technetwork/topics/winx64soft-089540.html
2) 解壓到一個目錄,並添加到環境變量
其他系統下使用也需要安裝這個工具,具體可以查看此文檔:
https://oracle.github.io/odpi/doc/installation.html#windows
cx_Oracle官網:https://oracle.github.io/python-cx_Oracle/