python3連接oracle數(shù)據(jù)庫(mac 10.13.6)

python連接oracle

1.OCI安裝

官網(wǎng)地址:
https://www.oracle.com/database/technologies/instant-client/macos-intel-x86-downloads.html

包地址:"Basic Light Package"
https://download.oracle.com/otn_software/mac/instantclient/193000/instantclient-basiclite-macos.x64-19.3.0.0.0dbru.zip

安裝命令與官網(wǎng)一樣:

cd ~
unzip instantclient-basiclite-macos.x64-19.3.0.0.0dbru.zip
mkdir ~/lib
ln -s ~/instantclient_19_3/libclntsh.dylib ~/lib/

2.安裝cx_oracle

https://www.cnblogs.com/songhouhou/p/11106367.html
$ pip3 install cx_oracle

例1:


import cx_Oracle

conn = cx_Oracle.connect('user', 'password', '192.168.3.150:1521/orcl')
cursor = conn.cursor()

print("連接成功!")

cursor.close()
conn.commit()
conn.close()

例2:

# -*- coding: utf-8 -*-

importcx_Oracle

import os
os.environ['NLS_LANG'] ='SIMPLIFIED CHINESE_CHINA.UTF8'  

# 設(shè)置編碼,不然select出來的數(shù)據(jù)如果有中文會提示gbk無法轉(zhuǎn)碼

conn = cx_Oracle.connect("username/password@localhost/sid")

# 獲取sid 方法,打開連接了oracle 的客戶端,執(zhí)行 selectinstance_namefromv$instance;  即所得

cursor = conn.cursor ()

cursor.execute ("查詢語句")

row = cursor.fetchmany(numRows=3)

print(row)

cursor.close()

conn.close()

** 測試結(jié)果完美 、2020-03-20、by: muye **

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容