Python 连接Oracle数据库

连接:python操作oracle数据库    python——连接Oracle数据库

python模块:cx_Oracle, DBUtil

 

大概步骤:

1. 下载模块 cx_Oracle (注意版本)

2. Windows下用easy_install.exe (Python安装目录下,Script目录中)先安装pip。

  pip要在path环境变量中

3. 在下载cx_Oracle模块的目录下,执行pip install  xxx 命令

4. 验证: Python执行 import cx_Oracle

 

连接方法:

方法一:用户名、密码和监听分开写        db=cx_Oracle.connect('username/password@host/orcl')
方法二:用户名、密码和监听写在一起    db=cx_Oracle.connect('username','password','host/orcl')
方法三:配置监听并连接               tns=cx_Oracle.makedsn('host',1521,'orcl')        db=cx_Oracle.connect('username','password',tns)

 

变量绑定:

x=c.execute('insert into demo(v) values(:1)',['nice'])
变量绑定和Oracle的动态SQL一样,都是冒号将占位符,即代码中的:1,对变量的复制就是传入一个List,即代码中的['nice']。有多少个变量,list中就应该对应多少个值,数目要一致,否则会报错。

调用存储过程 procedure: callproc

调用函数 function: callfunc

posted @ 2017-06-16 14:57  hahahahahahahaha  阅读(2335)  评论(0编辑  收藏  举报