需要先安裝 cx_Oracle, 可以通過這個網站上下載對應OS的cx_oracle: https://pypi.python.org/pypi/cx_Oracle/5.2.1
當然這還是不夠的,你還需要安裝oracle的instantclient, 官方網站是:
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
Windows上是一個zip包,需要把里面的lib 文件解壓到python的 site-package目錄下,不然會報dll load 的錯誤
簡單的一個例子
import cx_Oracle, os
conn = cx_Oracle.connect('BLITZSTAT/BLITZSTAT@10.27.10.18/orcl')
cursor = conn.cursor ()
sql_string = "SELECT distinct USERID FROM BLITZSTAT.STG_IS_SESSION_STATS"
cursor.execute(sql_string)
row = cursor.fetchall()
print len(row)
print row
conn.commit()
cursor.close ()
conn.close ()