python3.6连接oracle数据库
下载cx_Oracle模块模块:
https://pypi.python.org/pypi/cx_Oracle/5.2.1#downloads
这里下载的是源码进行安装
[root@oracle oracle]# tar xf cx_Oracle-5.2.1.tar.gz
[root@oracle oracle]# cd cx_Oracle-5.2.1
[root@oracle cx_Oracle-5.2.1]# python setup.py build
Traceback (most recent call last):
File "setup.py", line 170, in <module>
raise DistutilsSetupError("cannot locate an Oracle software " \
distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation
报错解决办法:在root用户的.bash_profile文件中添加oracle_home的环境变量
export ORACLE_HOME=/u01/app/product/11.2.0/dbhome_1
PATH=${ORACLE_HOME}/bin:$PATH:$HOME/bin
[root@oracle cx_Oracle-5.2.1]# source /root/.bash_profile
[root@oracle cx_Oracle-5.2.1]# echo ${ORACLE_HOME}
/u01/app/product/11.2.0/dbhome_1
然后继续build:
[root@oracle cx_Oracle-5.2.1]# python setup.py build
running build
running build_ext
后面输出信息省略
[root@oracle cx_Oracle-5.2.1]# python setup.py install
running install
running bdist_egg
后面输出信息省略
按照完成之后,进行验证模块:
>>> import cx_Oracle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory
解决办法:在root用户的.bash_profile文件中添加LD_LIBRARY_PATH的环境变量
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:/usr/local/lib
export LD_LIBRARY_PATH
[root@oracle cx_Oracle-5.2.1]# source /root/.bash_profile
[root@oracle cx_Oracle-5.2.1]# python
Python 3.6.1 (default, Jul 13 2017, 14:31:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
#!/usr/bin/python
#coding=utf8 # import module
import cx_Oracle as oracle # connect oracle database
db = oracle.connect('scott/redhat@192.168.223.138:1521/oracle.test') # create cursor
cursor = db.cursor() # execute sql
cursor.execute('select sysdate from dual') # fetch data
data = cursor.fetchone() print('Database time:%s' % data) # close cursor and oracle
cursor.close()
db.close()
[oracle@oracle scripts]$ python connectoracle.py
Database time:2017-08-04 10:20:39
#!/usr/bin/python
#coding=utf8 import cx_Oracle as oracle def oraclesql(cursor):
fp = open(r'/home/oracle/scripts/tablespace.sql')
fp_sql = fp.read()
cursor.execute(fp_sql)
data = cursor.fetchall()
return data if __name__ == '__main__':
ipaddr = "192.168.223.138"
username = "system"
password = "redhat"
oracle_port = "1521"
oracle_service = "oracle.test"
try:
db = oracle.connect(username+"/"+password+"@"+ipaddr+":"+oracle_port+"/"+oracle_service)
# 将异常捕捉,然后e就是抛异常的具体内容
except Exception as e:
print(e)
else:
cursor = db.cursor()
data = oraclesql(cursor)
for i in data:
print(i)
cursor.close()
db.close()
python3.6连接oracle数据库的更多相关文章
- Python3安装cx_Oracle连接oracle数据库实操总结
弄清版本,最重要!!! 首先安装配置时,必须把握一个点,就是版本一致!包括:系统版本,python版本,oracle客户端的版本,cx_Oracle的版本,然后安装配置就容易了! 如果已经安装Pyth ...
- python3.5连接oracle数据及数据查询
今天心血来潮研究下用python连接oracle数据库,看了一下demo,本以为很简单,从操作到成功还是有点坎坷,这里分享给大家,希望为后面学习的童鞋铺路. 一.首先按照cx_Oracle 二:在py ...
- Python3 连接 Oracle 数据库
Python3 连接 Oracle 数据库 需要导出一些稍微复杂的数据,用Python处理很方便 环境 Win10 Python 3.7.0 Oracle 11g 安装依赖 安装 cx_Oracle ...
- java连接Oracle数据库
Oracle数据库先创建一个表和添加一些数据 1.先在Oracle数据库中创建一个student表: create table student ( id ) not null primary key, ...
- 记录排查解决Hubble.Net连接Oracle数据库建立镜像库数据丢失的问题
起因 前几天在弄Hubble连接Oracle数据库,然后在mongodb中建立一个镜像数据库; 发现一个问题,原本数据是11W,但是镜像库中只有6w多条; 刚开始以为是没运行好,又rebuild了一下 ...
- NodeJs连接Oracle数据库
nodejs连接oracle数据库,各个平台的官方详情文档:https://github.com/oracle/node-oracledb/blob/master/INSTALL.md 我的nodej ...
- jdbc连接oracle数据库
/*** 通过改变配置文件来连接不同数据库*/package com.xykj.jdbc; import static org.junit.Assert.*; import java.io.Input ...
- 用VS连接oracle数据库时ORA-12504错误
在用VS2008连接oracle数据库时,可能会出现: ORA-12504: TNS: 监听程序在 CONNECT_DATA 中未获得 SERVICE_NAME 只需在web.config文件Data ...
- ASP.NET连接Oracle数据库的步骤详解(转)
ASP.NET连接Oracle数据库的步骤详解 本文我们主要介绍了ASP.NET连接Oracle数据库的步骤及每个步骤需要进行的设置,希望能够对您有所帮助. 在用ASP.NET开发应用程序时, ...
随机推荐
- 如何把EntityList转换成DataSet
public static DataSet ToDataSet<TSource>(this IList<TSource> list) { Type elementType = ...
- Pathwalks CodeForces - 960F(主席树 || 树状数组)
题意: 求树上最长上升路径 解析: 树状数组版: 998ms edge[u][w] 代表以u为一条路的终点的小于w的最长路径的路的条数 · 那么edge[v][w] = max(edge[u][w-1 ...
- HDU 5112 A Curious Matt (2014ACM/ICPC亚洲区北京站-重现赛)
A Curious Matt Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) ...
- import static和import的区别(转)
import static静态导入是JDK1.5中的新特性.一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....Cl ...
- 学习Spring Boot:(十二)Mybatis 中自定义枚举转换器
前言 在 Spring Boot 中使用 Mybatis 中遇到了字段为枚举类型,数据库存储的是枚举的值,发现它不能自动装载. 解决 内置枚举转换器 MyBatis内置了两个枚举转换器分别是:org. ...
- 【BZOJ1966】[AHOI2005]病毒检测(动态规划)
[BZOJ1966][AHOI2005]病毒检测(动态规划) 题面 BZOJ 洛谷 题解 我就蒯了一份代码随便改了改怎么就过了??? 从这道题目蒯的 代码: #include<iostream& ...
- ThinkPHP5项目目录
ThinkPHP5安装后(或者下载后的压缩文件解压后)可以看到下面的目录结构: tp5├─application 应用目录 ├─extend 扩展类库目录(可定义) ├─pu ...
- SpringBoot Logback日志配置
Logback的配置介绍: 1.Logger.appender及layout Logger作为日志的记录器,把它关联到应用的对应的context上后,主要用于存放日志对象,也可以定义日志类型.级别. ...
- ORACLE递归查询(适用于ID,PARENTID结构数据表)
Oracle 树操作(select…start with…connect by…prior) oracle树查询的最重要的就是select…start with…connect by…prior语法了 ...
- spring JMS在接收消息的时候总是报错
spring JMS在接收消息的时候总是报错 org.springframework.jms.UncategorizedJmsException: Uncategorized exception oc ...