您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)python如何實(shí)現(xiàn)MySQL增刪改查操作的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
代碼片段一
連接并執(zhí)行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) #執(zhí)行
取數(shù)據(jù)
print cursor.rowcount #取數(shù)據(jù) #fetchone()獲取一條數(shù)據(jù) #fetchany(3)獲取多條 #fetchall()#獲取客戶緩沖區(qū)的所有數(shù)據(jù) # 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
更新數(shù)據(jù)庫
# 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) # #執(zhí)行完后提交 # conn.commit() # #發(fā)生異常時回滾 # 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 銀行實(shí)例
#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 "出現(xiàn)問題了" + str(e) finally: conn.close()
感謝各位的閱讀!關(guān)于“python如何實(shí)現(xiàn)MySQL增刪改查操作”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。