Python连接oracle的几种方式

2012年3月6日 | 标签:
热度:

1.基本连接–使用Oracle tns alias

connection =cx_Oracle.connect("tp/tp@ocn_test")
#查看tns alias命令
cmd>tnsping ocn_test
TNS Ping Utility forLinux: Version 9.2.0.8.0-Production on 27-SEP-201110:47:48
Copyright (c) 1997, 2006, Oracle Corporation. Allrights reserved.
Used parameter files:
/opt/……/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL =TCP)(HOST =10.20.36.19)(PORT =1520))) (CONNECT_DATA =(SID =ocntest)))
OK (10msec)
2.用户输入密码连接
pwd =getpass.getpass()
connection =cx_Oracle.connect("tp",pwd,"ocn_test")
3.用户直接在Python命令中输入连接账号信息,格式如python script.py tp/tp@ocn_test
connection =cx_Oracle.connect(sys.argv[1])
4.使用Easy Connect语法,通过Drive连接数据库
connection =cx_Oracle.connect('tp','tp','10.20.36.19:1521/ocntest')
#or
connection =cx_Oracle.connect('tp/tp@10.20.36.19:1521/ocntest')
5.先使用DSN构成TNSNAME
tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ')
connection =cx_Oracle.connect('tp','tp',tns_name)
6.登陆as SYSDBA
connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSDBA)
#or as SYSOPER
connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSOPER)
p.s

python中的一些格式

#一般循环格式:
while<test1>:
<statements1>
if<test2> : break#Exit loop now,skip else
if<test3> : continue#Go to top of loop now,to test1
else:
<statements2> #Run if we didn't hit a 'break'
#for循环格式:
for<target> in<object> :
<statements>
else:
<statements>
feihu分享到:

          

相关博文

» 转载请注明来源:首页-->>Python连接oracle的几种方式
目前还没有任何评论.
您必须在 登录 后才能发布评论.