Python3 連接 Oracle 數據庫
需要導出一些稍微復雜的數據,用Python處理很方便
環境
Win10
Python 3.7.0
Oracle 11g
安裝依賴
安裝 cx_Oracle 模塊
pip install cx_Oracle
安裝 Oracle 客戶端
去官網下載 64 位 Windows 客戶端
instantclient-basic-windows.x64-11.2.0.4.0.zip
解壓安裝
並將解壓路徑配置到環境變量 PATH 中
E:\instantclient_11_2
代碼示例
import os
import cx_Oracle
import xlwt
# 解決 Oracle 亂碼問題
# 或 os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
conn = cx_Oracle.connect('username/password@127.0.0.1/orcl')
cursor = conn.cursor()
cursor.execute("select 'hello', 'world!' from dual")
rows = cursor.fetchall()
for row in rows:
print(row[0], row[1])
cursor.close()
conn.close()
輸出
hello world!