有些时候我们需要和程序交互,此时需要绑定量
下面两个例子简介在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