准备:
1、下载对应版本的oracle-instantclient(我这里是11.2.0.1.0)
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
2、安装
rpm -ivh oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
3、配置环境变量
#export ORACLE_BASE=/usr/lib/oracle
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
4、开始你的开发(我们用python)
开发:
python连接oracle数据库需要cx_Oracle
1、下载
http://sourceforge.net/projects/cx-oracle/
2、安装
我下载rpm包,安装很简单
rpm -ivh cx_Oracle-5.1.2-11g-py26-1.x86_64.rpm
3、开始你的开发吧
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
最后做了个封装:
import cx_Oracle
class oracleAPI:
# 构造函数
def __init__(self, user, pwd, ip, port, sid):
self.__user = user
self.__pwd = pwd
self.ip = ip
self.__port = port
self.__sid = sid
self.__dsn = self.get_dsn()
self.__db = self.get_conn()
self.__curs = self.get_curs()
# 析构函数
def __del__(self):
self.__curs.close()
print 'Cursor closed.'
self.__db.close()
print 'db closed.'
# 方法
def get_dsn(self):
dsn = cx_Oracle.makedsn(self.ip, self.__port, self.__sid)
return dsn
def get_conn(self):
db = cx_Oracle.connect(self.__user, self.__pwd, self.__dsn)
return db
def get_curs(self):
curs = self.__db.cursor()
return curs
def get_db_version(self):
return self.__db.version
def execute(self, sql):
result = self.__curs.execute(sql)
return result
def main():
user = '**ystem'
pwd = '**acle'
ip = '*.*.1.25'
port = '1521'
sid = '**yb'
oapi = oracleAPI(user, pwd, ip, port, sid)
print oapi.get_db_version()
result = oapi.get_curs().execute('select * from dba_users')
print result.fetchmany()
if __name__ == "__main__":
main()
补充:应用报错崩溃
ORA-24550: signal received: [si_signo=11] [si_errno=0] [si_code=1] [si_int=-257149584] [si_ptr=0x7f8af0ac3570] [si_addr=0x2740]
因为server上装的是oracle basic client所以没有sqlnet.ora所以要自己创建一个,而且要有环境变量
上边的环境变量要增加一个
export TNS_ADMIN=$ORACLE_HOME/network/admin
然后在admin文件夹里面创建一个文件名字为sqlnet.ora
内容:
DIAG_ADR_ENABLED=FALSE
DIAG_SIGHANDLER_ENABLED=FALSE
DIAG_DDE_ENABLED=FALSE
重启django应用,正常