python数据库操作

上传人:碎****木 文档编号:220860829 上传时间:2021-12-09 格式:DOCX 页数:3 大小:10.20KB
返回 下载 相关 举报
python数据库操作_第1页
第1页 / 共3页
python数据库操作_第2页
第2页 / 共3页
python数据库操作_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《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()

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号