首页 > 数据库技术 > 详细

Python 2.7.9 Demo - 019.01.CRUD oracle by cx_Oracle

时间:2015-07-02 23:56:53      阅读:388      评论:0      收藏:0      [点我收藏+]

 select

#coding=utf-8
#!/usr/bin/python
import cx_Oracle;

conn = None;
cursor = None;
try:
    conn = cx_Oracle.connect(username/password@xxx.xxx.xxx.xxx/sid);
    cursor = conn.cursor();
    cursor.execute(select t.empno, t.ename from scott.emp t);
    # 取回的是列表,列表中包含元组
    list = cursor.fetchall();
    print list;

    for record in list:
        print "Record %d is %s!" % (record[0], record[1]);
    
except Exception as e:
    print (Error : {}.format(e));
finally:
    cursor.close;
    print cursor closed.;
    conn.close;
    print connection closed.;
    

 

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

原文:http://www.cnblogs.com/nick-huang/p/4617298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!