1. Python 2.7 : https://www.python.org/download/releases/2.7/
2. Oracle Client : http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win32soft-098987.html
3. cx_oracle : https://pypi.python.org/pypi/cx_Oracle/5.2.1
4. Oracle Instance Client : http://www.oracle.com/technetwork/database/features/instant-client/index.html
4. Oracle Instance Client : http://www.oracle.com/technetwork/database/features/instant-client/index.html
上敘 1, 2 點基本安裝在本文就不說明,從 3, 4 進行說明
cx_oracle 主要是 python 擴充的 Library 用於 Oracle 資料庫的連線存取,而 Oracle Instance Client 則是連線時會需要使用到 oci.dll, oraociei12.dll 這兩個 dll 檔
首先先到 https://pypi.python.org/pypi/cx_Oracle/5.2.1 下載 cx_oracle,目前我使用的是 32bit windows 7 且 Python 2.7,因此選擇的版本是 32bit
安裝了了 cx_Oracle 才能在 python 中 import cx_Oracle 的 Library
import cx_Oracle
ip = 'IP'
port = portno
SID = 'TNS'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
con = cx_Oracle.connect( "ID", "PASSWORD", dsn_tns)
cursor = con.cursor()
cursor.close()
con.close()
import cx_Oracle
import os
os.environ['NLS_LANG'] = 'TRADITIONAL CHINESE_TAIWAN.ZHT16MSWIN950'
ip = 'IP'
port = portno
SID = 'TNS'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
con = cx_Oracle.connect( "ID", "PASSWORD", dsn_tns)
cursor = con.cursor()
cursor.close()
con.close()
print('Connect To Oracle')
以上即可完成 oracle 的連線
import cx_Oracle
import os
os.environ['NLS_LANG'] = 'TRADITIONAL CHINESE_TAIWAN.ZHT16MSWIN950'
ip = 'IP'
port = portno
SID = 'TNS'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
con = cx_Oracle.connect( "ID", "PASSWORD", dsn_tns)
cursor = con.cursor()
cursor.execute("""
select employee, emp_name from employee
""")
for value1, value2 in cursor:
print("Values:", value1, value2)
cursor.close()
con.close()
print('Connect To Oracle')
Python 要連線 Oracle 的程式相當簡單,但最大的困難是在如果未捉取 cx_Oracle 以及 Oracle Instance Client 需要的 dll 檔,但一但搞懂如何捉取對應的 cx_Oracle 與 Oracle Instance Client 其實很容易開發捉取 DB 的程式
沒有留言:
張貼留言