cx_Oracle模块学习之绑定变量
有些时候我们需要和程序交互,此时需要绑定量
下面两个例子简介在SELECT 和 DML 里面绑定变量的用法
SELECT 里面的绑定变量
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 里面的绑定变量
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()

浙公网安备 33010602011771号