ITPub博客

首页 > 数据库 > Oracle > cx_Oracle模块学习之绑定变量

cx_Oracle模块学习之绑定变量

原创 Oracle 作者:幕南风 时间:2014-07-03 14:09:39 0 删除 编辑


有些时候我们需要和程序交互,此时需要绑定量
下面两个例子简介在SELECT 和 DML 里面绑定变量的用法


SELECT 里面的绑定变量


[root@Ora10G py]# cat SelectBinding.py
#!/bin/env python
#coding=utf-8
#filename:execise binding in select
#Author:DBA_WaterBin

import cx_Oracle

conn=cx_Oracle.connect(\'hr/hr@orcl\')
cur=conn.cursor()

dept_id=raw_input(\'\n please input department ID you want to search:\')
dept_name=raw_input(\' please input department name you want to seaarch:\')

cur.execute(\'\'\'select * from departments
                where department_id=:id and
                      department_name=:name
            \'\'\',id=dept_id,name=dept_name
            )

rows=cur.fetchall()
print \'\ninformation are as following:\'
for row in rows:
  print row

cur.close()
conn.close()


DML 里面的绑定变量


[root@Ora10G py]# cat DmlBinding.py
#!/bin/env python
#coding=utf-8
#filename:execise binding in dml
#Author:DBA_WaterBin

import cx_Oracle

conn=cx_Oracle.connect(\'hr/hr@orcl\')
cur=conn.cursor()


cur.execute(\'\'\'insert into departments (department_id,department_name,manager_id,location_id) 
               values(:ID,:NAME,:MGR_ID,:LOC_ID)
              \'\'\', {\'ID\':555,\'NAME\':\'WaterBin\',\'MGR_ID\':110,\'LOC_ID\':7788}
           )
cur.close()
conn.commit()
conn.close()



By DBA_WaterBin

2013-09-09

good luck

<!-- Baidu Button BEGIN -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26515977/viewspace-1208219/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 外键和锁
下一篇: 安装MySQLdb-python时无法找到-lprobes_mysql处理一则
请登录后发表评论 登录
全部评论
腾讯数据库工程师

注册时间:2012-05-10

最新文章