欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)的MySQL增刪改查操作實例小結

 更新時間:2018年12月19日 10:06:58   作者:Tangzongyu123  
這篇文章主要介紹了python實現(xiàn)的MySQL增刪改查操作,結合實例形式總結分析了Python基本的mysql增刪改查及銀行賬號查詢等相關操作實現(xiàn)技巧,需要的朋友可以參考下

本文實例總結了python實現(xiàn)的MySQL增刪改查操作。分享給大家供大家參考,具體如下:

代碼片段一

連接并執(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 銀行實例

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

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python+MySQL數(shù)據(jù)庫程序設計入門教程》、《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學運算技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

  • Python正則表達式如何進行字符串替換實例

    Python正則表達式如何進行字符串替換實例

    Python正則表達式在使用中會經(jīng)常應用到字符串替換的代碼。這篇文章主要介紹了Python正則表達式如何進行字符串替換,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • 使用IPython下的Net-SNMP來管理類UNIX系統(tǒng)的教程

    使用IPython下的Net-SNMP來管理類UNIX系統(tǒng)的教程

    這篇文章主要介紹了使用IPython下的Net-SNMP來管理類UNIX系統(tǒng)的教程,本文來自于IBM官方網(wǎng)站技術文檔,需要的朋友可以參考下
    2015-04-04
  • Django模型驗證器介紹與源碼分析

    Django模型驗證器介紹與源碼分析

    這篇文章主要給大家介紹了關于Django模型驗證器與源碼分析的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Cpy和Python的效率對比

    Cpy和Python的效率對比

    這篇文章主要介紹了Cpy和Python的效率對比,本文用一個循環(huán) 100000000 遍的代碼對比了Cpy和Python運行效率測試,需要的朋友可以參考下
    2015-03-03
  • PyTorch中torch.utils.data.Dataset的介紹與實戰(zhàn)

    PyTorch中torch.utils.data.Dataset的介紹與實戰(zhàn)

    PyTorch是一個開源的Python機器學習庫,基于Torch,用于自然語言處理等應用程序,下面這篇文章主要給大家介紹了關于PyTorch中torch.utils.data.Dataset的介紹與實戰(zhàn),需要的朋友可以參考下
    2022-06-06
  • python實現(xiàn)簡單的井字棋小游戲

    python實現(xiàn)簡單的井字棋小游戲

    這篇文章主要為大家詳細介紹了python實現(xiàn)簡單的井字棋小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • python統(tǒng)計字符串中字母出現(xiàn)次數(shù)代碼實例

    python統(tǒng)計字符串中字母出現(xiàn)次數(shù)代碼實例

    這篇文章主要介紹了python統(tǒng)計字符串中字母出現(xiàn)次數(shù)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • Python subprocess模塊學習總結

    Python subprocess模塊學習總結

    從Python 2.4開始,Python引入subprocess模塊來管理子進程,以取代一些舊模塊的方法:如 os.system、os.spawn*、os.popen*、popen2.*、commands.*不但可以調用外部的命令作為子進程,而且可以連接到子進程的input/output/error管道,獲取相關的返回信息
    2014-03-03
  • Jupyter notebook 輸出部分顯示不全的解決方案

    Jupyter notebook 輸出部分顯示不全的解決方案

    這篇文章主要介紹了Jupyter notebook 輸出部分顯示不全的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 如何利用Boost.Python實現(xiàn)Python C/C++混合編程詳解

    如何利用Boost.Python實現(xiàn)Python C/C++混合編程詳解

    這篇文章主要給大家介紹了關于如何利用Boost.Python實現(xiàn)Python C/C++混合編程的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起看看吧
    2018-11-11

最新評論