Python学习之——Oracle数据库连接
一、安装Oracle客户端
1、下载对应安装文件,官网地址:http://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.html
这个是我自己下载好的:https://pan.baidu.com/s/10vbwHkvOrYRH3ewdkBuioQ

2、把下载好的文件放到Linux中
3、执行安装命令,因sdk包下的是压缩文件,所以直接解压放在安装目录,默认安装目录是/usr/lib/oracle/下
rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
4、设置环境变量
(1)编辑root用户下的profile文件
vi /etc/profile
(2)在文件的最后添加以下内容
export ORACLE_HOME=/usr/lib/oracle/11.2/client64 export TNS_ADMIN=$ORACLE_HOME/network/admin export NLS_LANG='simplified chinese_china'.ZHS16GBK export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH export PATH=$ORACLE_HOME/bin:$PATH
(3)执行source命令,使配置文件生效
source /etc/profile
二、安装cx_Oracle
ps:我是用pip安装的,因刚开始用非pip方式安装时,遇到如下错误,当时查了半天资料,说什么的都有,有说是Oracle的path配置不对,试过,好像没管用,具体没深究。
http://sourceforge.net/projects/cx-oracle/files/ cx_ORacle 旧版下载地址
https://pypi.org/project/cx_Oracle/#history cx_Oracle 新版下载地址
find / -name 'cx_Oracle.so' -print --查找文件命令
rpm -qa | grep -i cx_Oracle --查看安装的cx_Oracle得rmp
rpm -e cx_Oracle-5.1.2-1.x86_64 --卸载rmp
Traceback (most recent call last):
File "setup.py", line 187, in <module>
raise DistutilsSetupError("cannot locate Oracle include files")
distutils.errors.DistutilsSetupError: cannot locate Oracle include files
1、安装Python 的pip
(1)、下载文件,需要网络,也可以提前下载,放到指定目录。
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
(2)、执行安装
python get-pip.py
2、用pip安装cx_Oracle
(1)、安装cx_Oracle之前需要先建立一个链接libclntsh.so,如下:
cd /usr/lib/oracle/11.2/client64 ln -s libclntsh.so.11.1 libclntsh.so
(2)、执行安装
pip install cx_Oracle
(3)、测试,导入import cx_Oracle不报错,即说明安装成功,然后再测试连接数据库
import cx_Oracle
import cx_Oracle
conn = cx_Oracle.connect('jhinno/jhinno@192.168.0.188/jhinno')
cursor = conn.cursor ()
cursor.execute ("select sysdate from dual")
row = cursor.fetchone ()
print row
cursor.close ()
conn.close ()
三、封装的简单的类
#!/usr/bin/env python
#coding:utf-8
import cx_Oracle
#查询数据库
def sqlSelect(sql,db,*params):
cr=db.cursor()
cr.execute(sql,*params)
rs=cr.fetchall()
cr.close()
return rs
#增、删、改
def sqlDML(sql,db,*params):
cr=db.cursor()
cr.execute(sql,*params)
cr.close()
db.commit()
#批量插入
def sqlInsertSelect(sql,db,params):
cr=db.cursor()
cr.executemany(sql,params)
cr.close()
db.commit()
if __name__ =='__main__':
#查询数据1
empno="in ('7369','7499','7521')"
sql="select * from scott.emp a where a.empno %s" %empno
rows=db.sqlSelect(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'))
#查询数据2
#params = {'empno':'7499'}
#sql="select * from scott.emp a where a.empno in:empno"
#rows=db.sqlSelect(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'), params)
for row in rows:
print row
#批量插入数据(将查询的scott.emp数据插入到与之表结构一样的另一张表scott.emp_test)
sql="insert into scott.emp_test VALUES(:1,:2,:3,:4,:5,:6,:7,:8)"
db.sqlInsertSelect(sql,cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'),rows)
#删除
empno="in ('7369','7499','7521')"
sql="delete from scott.emp a where a.empno %s" %empno
db.sqlDML(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'))
#修改
empno="in ('7369','7499','7521')"
value='IT'
sql="update scott.emp a set a.job= %s where a.empno %s" %(value,empno)
db.sqlDML(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'))
Python学习之——Oracle数据库连接的更多相关文章
- Python学习--和 Oracle 交互(2)
当在 mac 电脑上用 Python 读取 oracle 数据库中的中文时,有可能返回数据为“?” 解决方案: 在数据库操作的函数前添加以下代码, import sysreload(sys)sys.s ...
- Python学习--和 Oracle 交互
python 连接oracle 数据库 1.安装 cx_oracle pip install cx_oracle 2.出现 cx_Oracle.DatabaseError: DPI-1047: 64- ...
- Java学习-006-三种数据库连接 MySQL、Oracle、sqlserver
此文主要讲述在初学 Java 时,常用的三种数据库 MySQL.Oracle.sqlserver 连接的源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源 ...
- 基于python实现Oracle数据库连接查询操作
使用python语言连接Oracle数据库配置 #coding:utf-8 import cx_Oracle as oracle db=oracle.connect('root/123456@192. ...
- [Python] 学习笔记之MySQL数据库操作
1 Python标准数据库接口DB-API介绍 Python标准数据库接口为 Python DB-API,它为开发人员提供了数据库应用编程接口.Python DB-API支持很多种的数据库,你可以选择 ...
- Python学习day5作业
目录 Python学习day5作业 ATM和购物商城 1. 程序说明 2. 基本流程图 3. 程序测试帐号 4. 程序结构: 5. 程序测试 title: Python学习day5作业 tags: p ...
- Python学习之==>面向对象编程(二)
一.类的特殊成员 我们在Python学习之==>面向对象编程(一)中已经介绍过了构造方法和析构方法,构造方法是在实例化时自动执行的方法,而析构方法是在实例被销毁的时候被执行,Python类成员中 ...
- Python学习day41-数据库(1)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- python入门灵魂5问--python学习路线,python教程,python学哪些,python怎么学,python学到什么程度
一.python入门简介 对于刚接触python编程或者想学习python自动化的人来说,基本都会有以下python入门灵魂5问--python学习路线,python教程,python学哪些,pyth ...
随机推荐
- python笔记8-多线程threading之封装式
执行函数 1.先写一个执行函数,用来实现做某件事情,不同的人吃火锅用一个参数people代替. # coding=utf-8 import threading import time def chiH ...
- Asp.Net MVC Identity 2.2.1 使用技巧(八)
一.添加管理链接 在View/Shared/_layout.cshtml,在页面导航上(28行)添加如下代码: @*通过身份验证并确认用户属于Admin角色显示管理菜单*@ @if (Request. ...
- lambdas vs. method groups
Update: Due to a glitch in my code I miscalculated the difference. It has been updated. See full his ...
- 深入浅出SharePoint2013——常用术语
CAS(Code Access Security)自定义代码访问安全性 Sandboxed solution 沙箱解决方案
- 利用describe( )中的count来检查数据是否缺省
#-*- coding: utf-8 -*- #在python的pandas库中,只需要读入数据,然后使用describe()函数就可以查看数据的基本情况 import pandas as pd in ...
- HomeBrew 使用国内数据源
使用中科大源 1.替换默认源 替换USTC镜像: cd "$(brew --repo)" git remote set-url origin https://mirrors.ust ...
- 《梦断代码》读书笔记 part3
第六章:搞掂设计方案 备份很重要. 必须从小项目开始,而且永远不要期望它变大,如果你这么想,就会做过度设计,把它想象得过于重要,更坏的情况是,你可能会被自己想象中的艰难工作所吓到.所以要从小 处起步, ...
- D3——scale
d3.scale 比例尺 “Scales are functions that map from an input domain to an output range” Domains 定义域 和 R ...
- Python新式类和经典类的区别
@Python新式类和经典类的区别 class ClassicClass(): pass class NewStyleClass(object): pass x1 = ClassicClass() x ...
- iOS js 使用与JSContext
JSContext:js执行环境,包含了js执行时所需要的所有函数和对象: js执行时,会在执行环境搜索需要的函数然后执行,或者保存传入的变量或函数: JSContext *jsContext = [ ...