step1:下載cx_Oracle模塊,cmd--pip install cx_Oraclehtml
step2:數據庫
1 import cx_Oracle #引用模塊cx_Oracle 2 conn=cx_Oracle.connect('truck/******@10.74.**.**:****/****') #鏈接數據庫 3 c=conn.cursor() #獲取cursor 4 x=c.execute('select sysdate from dual') #使用cursor進行各類操做 5 x.fetchone() 6 c.close() #關閉cursor 7 conn.close() #關閉鏈接
報錯:cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "The specified module could not be found".oracle
緣由:本機裝的Python、cx_Oracle都是64位的,Navicat鏈接的Oracle instantclient版本爲32位的,因此鏈接報錯。fetch
解決方案:下載64位 instantclient---http://jvniu.jb51.net:81/201708/tools/instantclientx64_jb51.rar 或者 http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.htmlspa
操做:下載結束後,解壓至 Navicat根目錄,添加環境變量,重啓鏈接腳本。.net
報錯:listener does not currently know of service requested in connect descriptor3d
緣由:參數理解錯誤code
#conn=cx_Oracle.connect(‘用戶名/密碼@主機ip地址:端口號/Service Name(SID)') conn=cx_Oracle.connect('truck/******@10.74.**.**:****/****')
正確輸入參數以後,數據庫鏈接成功htm
Python鏈接Oracle若是有中文,可能會出亂碼,可經過如下方法解決blog
1 import os 2 os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
同時,在Python腳本中,添加一行代碼
# -*- coding: utf-8 -*-