最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3連接MySQL(pymysql)模擬轉(zhuǎn)賬實(shí)現(xiàn)代碼

 更新時(shí)間:2016年05月24日 16:11:32   作者:沙拉虎  
這篇文章主要介紹了Python3連接MySQL(pymysql)模擬轉(zhuǎn)賬實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python3連接MySQL模擬轉(zhuǎn)賬的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下

# coding:utf8
import sys
import pymysql
 
class TransferMoney(object):
  def __init__(self,conn):
    self.conn=conn
 
  def check_acct_available(self,acctid):
    cursor = self.conn.cursor()
    try:
      sql="select * from account where acctid=%s" % acctid
      cursor.execute(sql)
      print ("check_acct_available:" + sql)
      rs = cursor.fetchall()
      if len(rs) ! = 1:
        raise Exception("賬號(hào)%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("賬號(hào)%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("賬號(hào)%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 ("add_money:"+sql)
      if cursor.rowcount ! = 1:
        raise Exception("賬號(hào)%s加款失敗" % acctid)
    finally:
      cursor.close()
 
 
  def transfer(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
 
 
if __name__ == "__main__":
  source_acctid = sys.argv[1]
  target_acctid = sys.argv[2]
  money = sys.argv[3]
 
 
  conn = pymysql.Connect(
            host = 'localhost',
            unix_socket = "..mysql/mysql.sock",
            port = 3306,
            user = 'root',
            passwd = '',
            db = 'python_db',
                    )
  tr_money = TransferMoney(conn)
 
  try:
    tr_money.transfer(source_acctid,target_acctid,money)
  except Exception as e:
    print ("出現(xiàn)問題" + str(e))
  finally:
    conn.close()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

肥东县| 余姚市| 岐山县| 孝昌县| 儋州市| 乌拉特中旗| 靖西县| 墨脱县| 松原市| 吴桥县| 霍林郭勒市| 宁国市| 抚顺县| 大姚县| 阿拉善左旗| 额敏县| 武城县| 成都市| 湘潭市| 江西省| 泸水县| 宁阳县| 汾西县| 宜君县| 乐昌市| 平利县| 沅陵县| 南阳市| 任丘市| 新郑市| 牙克石市| 凤凰县| 色达县| 高邮市| 湟中县| 太仓市| 松江区| 东阿县| 赤壁市| 汤原县| 英德市|