Oracel安装(windows 64位)
1. 首先确定版本。
2. 下载instantclient,下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
解压到指定目录,加入环境变量
变量名 ORACLE_HOME
变量名 TNS_ADMIN
变量名 NLS_LANG
SIMPLIFIED CHINESE_CHINA.ZHS16GBK
变量名 Path
在解压的oracle目录instantclient_11_2/network/admin创建tnsnames.ora文件,写入连接数据库地址
3. 下载cx_Oracle, 这个是一个Python的扩展包,用来访问Oracle数据库的。 https://pypi.python.org/pypi/cx_Oracle/5.2.1
Oracel安装(centos 6.6)
1. 首先确定版本。
2. 下载instantclient-basic。下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
instantclient-basic-linux.x64-11.2.0.4.0.zip
instantclient-sdk-linux.x64-11.2.0.4.0.zip
instantclient-sqlplus-linux.x64-11.2.0.4.0.zip
3. 安装和环境变量配置
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip unzip instantclient-sqlplus-linux.x64-11.2.0.4.0.zip
instantclient_11_2目录下network/admin创建tnsnames.ora文件,写入连接数据库地址
export ORACLE_HOME=/home/oracle/instantclient_11_2 export PATH=$ORACLE_HOME:$PATH export TNS_ADMIN=$ORACLE_HOME/network/admin export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
创建软连接
ln -s /home/oracle/instantclient_11_2/libclntsh.so.11.1 /home/oracle/instantclient_11_2/libclntsh.so
4. 下载cx_Oracle, 这个是一个Python的扩展包,用来访问Oracle数据库的。 https://pypi.python.org/pypi/cx_Oracle/5.2.1
tar -xvf cx_Oracle-5.2.1.tar.gz cd cx_Oracle-5.2.1 python setup.py install
5.导入测试
import cx_Oracle
代码
# -*- coding: utf-8 -*- import cx_Oracle from east_web import settings from east_web.log_config import django_logger as logger class Oracle(object): def __init__(self): self.__username = settings.ORACLE_CONN['USERNAME'] self.__password = settings.ORACLE_CONN['PASSWORD'] self.__host = settings.ORACLE_CONN['HOST'] self.__port = settings.ORACLE_CONN['PORT'] self.__tnsname = settings.ORACLE_CONN['TNSNAME'] self.cursor = self.connect() def connect(self): """ 连接数据库, 获取游标 语法: cx_Oracle.connect('username','pwd','IP/HOSTNAME:PORT/TNSNAME') 或 cx_Oracle.makedsn(IP,PORT,TNSNAME) cx_Oracle.connect(username,pwd,dsn) """ try: dsn = cx_Oracle.makedsn(self.__host, self.__port, self.__tnsname) conn = cx_Oracle.connect(self.__username, self.__password, dsn) # self.__conn = cx_Oracle.connect(self.__username, self.__password, # self.__host + ':' + self.__port + '/' + self.__service_name) cursor = conn.cursor() return cursor except Exception as err: logger.error(err) def ogg_process_lag(self, sql): """ :param sql: :return: 执行sql """ self.cursor.execute(sql) def fetchone(self): """ :return: 返回第一条数据 """ res_one = self.cursor.fetchone() return res_one def fetchmang(self, n): """ :param n: :return: 返回第n条数据 """ res_mang = self.cursor.fetchmany(n) return res_mang def fetchall(self): """ :return: 返回所有数据 """ res_all = self.cursor.fetchall() return res_all def main(): oracle = Oracle() oracle.ogg_process_lag("SELECT * FROM table") print oracle.fetchmang(7) if __name__ == '__main__': main()