圆月山庄资源网 Design By www.vgjia.com
本文实例总结了python实现的MySQL增删改查操作。分享给大家供大家参考,具体如下:
代码片段一
连接并执行sql
#encoding:UTF-8 import MySQLdb conn = MySQLdb.Connect( host = '127.0.0.1', port = 3306, user = 'root', passwd='123456', db='imooc', charset='utf8' ) cursor = conn.cursor() print conn print cursor sql = "select * from user" cursor.execute(sql) #执行
取数据
print cursor.rowcount #取数据 #fetchone()获取一条数据 #fetchany(3)获取多条 #fetchall()#获取客户缓冲区的所有数据 # rs = cursor.fetchone() # print rs # # rs = cursor.fetchmany(2) # print rs # # rs = cursor.fetchall() # print rs # rs = cursor.fetchall() # for row in rs: # print "userid=%s,username=%s" % row
更新数据库
# sql_insert = "insert into user(userid,username) values(10,'name10')" # sql_update = "update user set username='name91' where userid=9" # sql_delete = "delete from user where userid<3" # cursor.execute(sql_insert) # cursor.execute(sql_update) # cursor.execute(sql_delete) # #执行完后提交 # conn.commit() # #发生异常时回滚 # try: # sql_insert = "insert into user(userid,username) values(10,'name10')" # sql_update = "update user set username='name91' where userid=9" # sql_delete = "delete from user where userid<3" # cursor.execute(sql_insert) # cursor.execute(sql_update) # cursor.execute(sql_delete) # conn.commit() # except Exception as e: # print e # conn.rollback() cursor.close() conn.close()
代码片段2 银行实例
#coding:UTF-8 import sys import MySQLdb class TransferMoney(object): def __init__(self,conn): self.conn = conn def tranfer(self,source_acctid,target_acctid,money): try: self.check_acct_available(source_acctid) self.check_acct_available(target_acctid) self.has_enough_money(source_acctid,money) self.reduce_money(source_acctid,money) self.add_money(target_acctid,money) self.conn.commit() except Exception as e: self.conn.rollback() raise e def check_acct_available(self, acctid): cursor = self.conn.cursor() try: sql = "select * from account where acctid=%s"%acctid cursor.execute(sql) rs = cursor.fetchall() if len(rs)!=1: raise Exception("账号%s不存在"%acctid) finally: cursor.close() def has_enough_money(self, acctid, money): cursor = self.conn.cursor() try: sql = "select * from account where acctid=%s and money>%s" % (acctid,money) cursor.execute(sql) print "has_enough_money:"+sql rs = cursor.fetchall() if len(rs) != 1: raise Exception("账号%s没有足够的钱" % acctid) finally: cursor.close() def reduce_money(self, acctid, money): cursor = self.conn.cursor() try: sql = "update account set money=money-%s where acctid=%s" % (money,acctid) cursor.execute(sql) print "reduce_money:"+sql if cursor.rowcount != 1: raise Exception("账号%s减款失败" % acctid) finally: cursor.close() def add_money(self, acctid, money): cursor = self.conn.cursor() try: sql = "update account set money=money+%s where acctid=%s" % (money, acctid) cursor.execute(sql) print "reduce_money:" + sql if cursor.rowcount != 1: raise Exception("账号%s加款失败" % acctid) finally: cursor.close() if __name__ == "__main__": source_acctid = sys.argv[1] target_acctid = sys.argv[2] money = sys.argv[3] conn = MySQLdb.Connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='imooc', charset='utf8' ) tr_money = TransferMoney(conn) try: tr_money.tranfer(source_acctid,target_acctid,money) except Exception as e: print "出现问题了" + str(e) finally: conn.close()
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python+MySQL数据库程序设计入门教程》、《Python常见数据库操作技巧汇总》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月06日
2024年11月06日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]