使用python管理oracle数据库
当用上python后,感觉还是很方便的,比如管理oracle,不用再去安装个客户端,直接python脚本执行下,就完成操作了.
系统:centos 7(64位)
软件环境:python 2.7.5
1.安装cx_oracle模块
pip install cx_Oracle
2.安装依赖包
wget https://github.com/bumpx/oracle-instantclient/raw/master/oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
wget https://github.com/bumpx/oracle-instantclient/raw/master/oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
yum install libaio -y
vim .bash_profile
vim .bash_profile
在最下面加上一句:
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib:$LD_LIBRARY_PATH
vim /etc/profile
在最下面加上一句:
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib:$LD_LIBRARY_PATH
source /etc/profile
在最下面加上一句:
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib:$LD_LIBRARY_PATH
source /etc/profile
echo "/usr/lib/oracle/12.2/client64/lib" > /etc/ld.so.conf.d/oracle-instantclient.conf
ldconfig
ldconfig
3.使用python连接oracle
cat /root/soft_shell/sql.py
#!/usr/bin/env python # -*- coding: utf8 -*- import cx_Oracle import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' conn = cx_Oracle.connect('user', 'password', 'ip:1521/ORCL') cursor = conn.cursor() cursor.execute("SELECT * FROM db.table where USERID='1' order by SIGNDATE DESC") row = cursor.fetchone() print row row = cursor.fetchone() print row cursor.close() conn.close()
上面这个脚本就是一个简单的查询,是不是很简单,剩下的就交给各位自己去操作了,比如批量插入数据,修改数据等等,这里我就不过多演示了.
评论: