时间:2021-07-01 10:21:17 帮助过:3人阅读
insert
#coding=utf-8 #!/usr/bin/python import cx_Oracle; import time; conn = None; cursor = None; try: conn = cx_Oracle.connect(‘username/password@xxx.xxx.xxx.xxx/sid‘); cursor = conn.cursor(); tuple = (‘1001‘, ‘Nick Huang‘); cursor.execute("insert into scott.emp (empno, ename) values (:1, :2)", tuple); conn.commit(); print ‘Insert successfully.‘; except Exception as e: print (‘Error : {}‘.format(e)); finally: cursor.close; print ‘cursor closed‘; conn.close; print ‘connection closed‘;
update
#coding=utf-8 #!/usr/bin/python import cx_Oracle; import time; conn = None; cursor = None; try: conn = cx_Oracle.connect(‘username/password@xxx.xxx.xxx.xxx/sid‘); cursor = conn.cursor(); tuple = (‘Robin Chen‘, ‘1001‘); cursor.execute("update scott.emp t set t.ename = :1 where t.empno = :2", tuple); conn.commit(); print ‘Update successfully.‘; except Exception as e: print (‘Error : {}‘.format(e)); finally: cursor.close; print ‘cursor closed‘; conn.close; print ‘connection closed‘;
delete
#coding=utf-8 #!/usr/bin/python import cx_Oracle; import time; conn = None; cursor = None; try: conn = cx_Oracle.connect(‘username/password@xxx.xxx.xxx.xxx/sid‘); cursor = conn.cursor(); param_map = {‘id‘ : ‘1001‘}; cursor.execute("delete from scott.emp t where t.empno = :id", param_map); conn.commit(); print ‘Delete successfully.‘; except Exception as e: print (‘Error : {}‘.format(e)); finally: cursor.close; print ‘cursor closed‘; conn.close; print ‘connection closed‘;
Python 2.7.9 Demo - 019.01.CRUD oracle by cx_Oracle
标签: