Mac python3.6 利用cx_Oracle 鏈接和操做Oracle數據庫

首先要用cx_Oracle訪問遠程oralce服務,須要安裝oracle客戶端 instantclient 具體操做能夠看我例外一篇文章」Mac OS 安裝配置 instant clientpython

1. 安裝cx_Oracle數據庫

pip/pip3 install cx_Oracle

我這裏是python3和python2 雙環境,因此使用pip3 安裝到python3環境下。bash

2. 使用oracle

很少說直接上代碼:fetch

import cx_Oracle #引入模塊

#查詢
conn = cx_Oracle.connect('user/password@ip:port/數據庫服務名稱') #獲取鏈接
cursor = conn.cursor() # 獲取cursor
cursor.execute('SELECT * FROM TBL_USER') # 執行操做
one = cursor.fetchone() #獲取返回信息
print('name:%s' % one) #打印信息
cursor.close() #關閉cursor
conn.close() # 關閉鏈接

#插入
cursor.execute('INSERT INTO TBL_USER(name,password) VALUES ("name","password")')
one = cursor.fetchone()
cursor.close()
conn.commit()
conn.close()
相關文章
相關標籤/搜索