摘要:連接當前環境下載安裝包由于我本地版本是所以選擇是版本參考安裝包列表安裝安裝查看當前引入模塊庫的路徑檢查安裝路徑,如果文件不在以上引入路徑內需要將該文件拷貝到以上引入路徑所以我這里不需要進行拷貝操作
Python連接Oracle
1. 下載安裝包cx_Oracle當前環境:Linux Centos 7
由于我本地Python版本是2.7,所以選擇是2.7版本
wget https://pypi.python.org/packages/e1/18/00987c6a9af9568ee87d1fcba877407684a3f1b87515e5eb82d5d5acb9ff/cx_Oracle-6.0rc1-py27-1.x86_64.rpm#md5=ff8f5cce07bd5ec8ed3ce5c79d451474
參考:
2. 安裝cx_Oraclepython-cx_Oracle
安裝包列表
安裝:
rpm -ivh cx_Oracle-6.0rc1-py27-1.x86_64.rpm
查看當前python引入模塊庫的路徑
[root ~]# python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print sys.path ["", "/usr/lib/python2.7/site-packages/AliyunUtil-0.0.1-py2.7.egg", "/usr/lib/python2.7/site-packages/cloud_init-0.7.6-py2.7.egg", "/usr/lib64/python27.zip", "/usr/lib64/python2.7", "/usr/lib64/python2.7/plat-linux2", "/usr/lib64/python2.7/lib-tk", "/usr/lib64/python2.7/lib-old", "/usr/lib64/python2.7/lib-dynload", "/usr/lib64/python2.7/site-packages", "/usr/lib/python2.7/site-packages"]
檢查安裝路徑,如果cx_Oracle.so文件不在以上python引入路徑內需要將該文件拷貝到以上python引入路徑,所以我這里不需要進行拷貝操作
[root ~]# rpm -qa | grep cx_Oracle | xargs rpm -ql /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/PKG-INFO /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/SOURCES.txt /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/dependency_links.txt /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/top_level.txt /usr/lib64/python2.7/site-packages/cx_Oracle.so3. 下載Oracle Library
由于Oracle官網下載東西存在Auth認證,所以簡單點就點擊選中的rpm包保存在windows上,然后上傳到linux上即可
注意:
4. 安裝Oracle Library下載的版本要和連接的Oracle基本一致,我需要連接的Oracle版本是11.2,所以我下載的是oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
操作系統要和之前下載的cx_Oracle要一致,這很重要!!!我在這個上面栽了跟頭,花了比較多的時間。。。所以我需要下載64位的
在Oracle官網下載東西你得先勾選Accept License Agreement,否則你下載不了
其次你得有Oracle的賬號
rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm5. 配置環境變量
修改Oracle安裝路徑
將client64改為client,至于為什么要改暫時還不清楚,只知道不改的話會報錯Oracle Client library cannot be loaded: libclntsh.so: cannot open shared object file: No such file or directory
cd /usr/lib/oracle/11.2/ mv client64 client
打開~/.bashrc,追加環境變量路徑
vim ~/.bashrc
配置環境變量
// 查看Oracle安裝路徑 [root@iZbp180vortqapb7yf8217Z ~]# rpm -qa | grep oracle | xargs rpm -ql /usr/lib/oracle/11.2/client64/bin/adrci /usr/lib/oracle/11.2/client64/bin/genezi /usr/lib/oracle/11.2/client64/lib/libclntsh.so.11.1 /usr/lib/oracle/11.2/client64/lib/libnnz11.so /usr/lib/oracle/11.2/client64/lib/libocci.so.11.1 /usr/lib/oracle/11.2/client64/lib/libociei.so /usr/lib/oracle/11.2/client64/lib/libocijdbc11.so /usr/lib/oracle/11.2/client64/lib/ojdbc5.jar /usr/lib/oracle/11.2/client64/lib/ojdbc6.jar /usr/lib/oracle/11.2/client64/lib/xstreams.jar // 追加ORACLE_HOME環境變量路徑 export ORACLE_HOME=/usr/lib/oracle/11.2/client export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
生效配置
source ~/.bashrc6. 檢查是否可以引入cx_Oracle
import沒有報錯,說明之前的辛苦沒有白費
[root client64]# python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cx_Oracle >>>7. 連接Oracle
一個插入n條數據的Python腳本:
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import cx_Oracle import time rows = int(sys.argv[1]) con = cx_Oracle.connect("test/test@sampledb") cur = con.cursor() cur.execute("SELECT MAX(ID) FROM PTTEST") max_id = cur.fetchone()[0] or 0 start_id = max_id + 1 for i in xrange(rows): cur.execute("INSERT INTO PTTEST (ID, NAME, AGE, GENDER, SALARY)VALUES(%d, "pt", 15, "male", 50000)" % int(i + start_id)) if i % 100 == 0: time.sleep(3) print "Insert rows [%d]:%d" % (int(time.time()), i) con.commit() cur.close() con.close()
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/44449.html
Connecting Python to Oracle Cloud Database There are three flavors of Oracle Databases hosted on Oracle Cloud Infrastructure: Bare Mental, VM, and Exadata Autonomous Data Warehouse (ADWC) Autonomous ...
Connecting Python to Oracle Cloud Database There are three flavors of Oracle Databases hosted on Oracle Cloud Infrastructure: Bare Mental, VM, and Exadata Autonomous Data Warehouse (ADWC) Autonomous ...
閱讀 2016·2021-11-12 10:36
閱讀 1865·2021-11-09 09:49
閱讀 2591·2021-11-04 16:12
閱讀 1144·2021-10-09 09:57
閱讀 3235·2019-08-29 17:24
閱讀 1909·2019-08-29 15:12
閱讀 1272·2019-08-29 14:07
閱讀 1285·2019-08-29 12:53