1.系统环境简介
1)客户端
OS: Windows 7 SP1 x86
Python: ActivePython 2.7.2.5 32bit
Python Oracle Module: cx_Oracle-5.1.2-11g.win32-py2.7.msi
2)Oracle数据库服务器:
SELS 11 SP1 X64
Oracle 11.2 企业版 X64
建立数据库实例testdb,启动listener程序
2.客户端配置
1)安装python软件,cx_Oracle模块程序
2)下载并解压oracle客户端软件
instantclient-basic-win32-11.2.0.1.0.zip
解压到“E:\oracleLinux”,$IRACLE_HOME 目录下建立NETWORK\ADMIN,
ADMIN目录下新建tnsnames.ora文件,内容如下:
testtns =( DESCRIPTION =
( ADDRESS_LIST =
( ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.10)(PORT = 1521)))
(CONNECT_DATA =
(SERVICE_NAME = testdb))
)
3)添加系统环境变量
ORACLE_HOME值为“E:\oracleLinux\instantclient_11_2”
3.windows命令行执行SQL语句
>>> import cx_Oracle
>>> conn = cx_Oracle.connect('testuser/testpwd@testtns')
>>> cur = conn.cursor()
>>> cur.execute('select count(*) from test')
<__builtin__.OracleCursor on <cx_Oracle.Connection to testuser/testpwd>>
>>> for row in cur:
... print row
...
(6,)
>>>