《python数据库操作》由会员分享,可在线阅读,更多相关《python数据库操作(3页珍藏版)》请在金锄头文库上搜索。
1、数据库连接# coding:gbkCreated on 2010-7-15author: Administrator import cx_Oracle dsn=cx_Oracle.makedsn(150.150.1.45,1521,dx) conn=cx_Oracle.connect(test,123,dsn)print 连接oracle成功!try:cur=conn.cursor()sql=select * from portt_type rr=cur.execute(sql) row=cur.fetchall()for x in row:print 记录,x0,x1 cur.close()
2、except Exception,e: print eelse:print 一切正常finally:conn.close() print 连接关闭数据库操作# coding:gbkCreated on 2010-7-15author: Administrator import cx_Oracle#用户名,密码,服务名db=cx_Oracle.connect(user,pass,tns_name)c=db.cursor(); #建表c.execute(create table test(a int,b varchar2(100) )#建序列c.execute(create sequence st
3、est) #插入数据c.execute(insert into test(a,b) values(stest.nextval,Python) )c.execute(insert into test(a,b) values(stest.nextval,Oracle) ) #检索插入的数据sql = select * from testr = c.execute(sql) row = c.fetchone() while row: (a,b)=(row0,row1) print a,b row=c.fetchone()#删除插入的数据sql = delete from test where a=1c.execute(sql) #检索sql = select * from testr = c.execute(sql) row = c.fetchone() while row: (a,b)=(row0,row1) print a,b row=c.fetchone()#清理表和序列c.execute(drop table test) c.execute(drop sequence stest) mit()db.close()