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

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

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

本文實例總結(jié)了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()

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

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

相關(guān)文章

  • 基于Python繪制子圖及子圖刻度的變換等的問題

    基于Python繪制子圖及子圖刻度的變換等的問題

    這篇文章主要介紹了基于Python繪制子圖及子圖刻度的變換等的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python實現(xiàn)TCP/IP協(xié)議下的端口轉(zhuǎn)發(fā)及重定向示例

    Python實現(xiàn)TCP/IP協(xié)議下的端口轉(zhuǎn)發(fā)及重定向示例

    這篇文章主要介紹了Python實現(xiàn)TCP/IP協(xié)議下的端口轉(zhuǎn)發(fā)及重定向示例,以一個webpy站點(diǎn)在本機(jī)的兩個端口雙向通信下演示,需要的朋友可以參考下
    2016-06-06
  • pytorch多GPU并行運(yùn)算的實現(xiàn)

    pytorch多GPU并行運(yùn)算的實現(xiàn)

    這篇文章主要介紹了pytorch多GPU并行運(yùn)算的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Python如何拆分ZIP文件

    Python如何拆分ZIP文件

    這篇文章主要介紹了Python如何拆分ZIP文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 用Python繪制一個仿黑洞圖像

    用Python繪制一個仿黑洞圖像

    黑洞圖像大家都知道,畢竟前幾年剛發(fā)布的時候曾火遍全網(wǎng),甚至都做成表情包了。本文就來用Python繪制一個仿黑洞圖像,希望大家能夠喜歡
    2023-02-02
  • 用Python制作mini翻譯器的實現(xiàn)示例

    用Python制作mini翻譯器的實現(xiàn)示例

    這篇文章主要介紹了用Python制作mini翻譯器的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python 常見加密操作的實現(xiàn)

    Python 常見加密操作的實現(xiàn)

    這篇文章主要介紹了Python 常見加密操作的實現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • 如何利用itertuples對DataFrame進(jìn)行遍歷

    如何利用itertuples對DataFrame進(jìn)行遍歷

    這篇文章主要介紹了如何利用itertuples對DataFrame進(jìn)行遍歷問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Windows環(huán)境打包python工程為可執(zhí)行程序的詳細(xì)過程

    Windows環(huán)境打包python工程為可執(zhí)行程序的詳細(xì)過程

    我的開發(fā)環(huán)境是windows7,然后系統(tǒng)是64位,安裝的python和wxpython都是32位的,本文記錄我怎樣用pyinstaller打包我用python開發(fā)的工程,在網(wǎng)上搜索了很多資源,基本上都是不全的,所以我在這兒記錄一下這個比較完整的過程,一起看看吧
    2024-01-01
  • python腳本監(jiān)控Tomcat服務(wù)器的方法

    python腳本監(jiān)控Tomcat服務(wù)器的方法

    這篇文章主要介紹了利用python腳本監(jiān)控Tomcat服務(wù)器的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07

最新評論

桂平市| 龙南县| 永宁县| 洛川县| 海安县| 卢龙县| 林州市| 醴陵市| 孙吴县| 凤山县| 杭锦后旗| 象州县| 曲麻莱县| 台中市| 甘谷县| 博乐市| 广昌县| 乡城县| 莱州市| 郑州市| 永城市| 彭水| 莱州市| 西平县| 安溪县| 东源县| 泊头市| 濮阳县| 阿勒泰市| 芜湖县| 德格县| 科尔| 辰溪县| 清涧县| 建始县| 瓮安县| 靖宇县| 泸定县| 岱山县| 九江县| 达尔|