免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
[每日一題]20、Python操作MySQL

第一時間收到精彩推送!

Python Every Day, 第 20 天


    
在工作中,不管Web開發(fā)或者爬蟲,數(shù)據(jù)分析等凡是與數(shù)據(jù)有接觸的都離不開數(shù)據(jù)庫。數(shù)據(jù)庫又分為關(guān)系型數(shù)據(jù)庫還是非關(guān)系型數(shù)據(jù)庫,關(guān)系型數(shù)據(jù)庫中最常用應(yīng)該就是MySQL了。
在Python中可以通過PyMySQL庫完成對其的操作。

安裝PyMySQL

pip3 install pymysql

操作過程大概分為如下幾步

0、在MySQL中創(chuàng)建庫和表
1、創(chuàng)建數(shù)據(jù)庫連接
2、創(chuàng)建cursor,用于執(zhí)行sql
3、編寫并執(zhí)行SQL
4、獲取數(shù)據(jù)
5、關(guān)閉連接
通過下面一個簡單的例子給大家演示一下

創(chuàng)建test庫,并在該庫中創(chuàng)建fund_info(資產(chǎn)信息表)
CREATE DATABASE `test` ;
use test;
CREATE TABLE `fund_info` (
  `id` int(11NOT NULL AUTO_INCREMENT,
  `account` varchar(50NOT NULL COMMENT '賬號',
  `amount` decimal(10,2DEFAULT NULL COMMENT '總金額',
  `consume` decimal(10,2DEFAULT '0.00' COMMENT '消費金額',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `modify_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
ENGINE=InnoDB DEFAULT CHARSET=latin1
具體操作(增刪改查)看示例代碼吧,在代碼中盡可能的都注釋了。
import pymysql

# 創(chuàng)建MySQL連接
connection = pymysql.connect(host='127.0.0.1',  # MySQL服務(wù)器地址
                             port=3306,         # 服務(wù)端口號
                             user='root',       # MySQL用戶名
                             password='root',   # MySQL密碼
                             db='test',         # 要操作的數(shù)據(jù)庫名稱
                             charset='utf8mb4'# 連接編碼
# 創(chuàng)建cursor, 用于執(zhí)行sql
cursor = connection.cursor()

#  增加兩條記錄。
insert_sql = 'insert into fund_info (account, amount) values ('abc@163.com', 100.00)'
insert_sql1 = 'insert into fund_info (account, amount) values ('zxc@163.com', 99.00)'
# 執(zhí)行上面兩個sql,
cursor.execute(insert_sql)
cursor.execute(insert_sql1)
# 執(zhí)行增 刪 改時 需要commit,不然不會生效
connection.commit()

# 查詢剛才插入的兩條數(shù)據(jù)
cursor.execute('select * from fund_info')
# fetchall 查看執(zhí)行的全部結(jié)果,返回一個tuple
result_all = cursor.fetchall()
''' 輸出:
result_all : 
((2, 'abc@163.com', Decimal('100.00'), Decimal('0.00'), datetime.datetime(2019, 8, 7, 16, 57, 49), datetime.datetime(2019, 8, 7, 16, 57, 49)), 
(3, 'zxc@163.com', Decimal('99.00'), Decimal('0.00'), datetime.datetime(2019, 8, 7, 16, 57, 49), datetime.datetime(2019, 8, 7, 16, 57, 49)))
'''

print('result_all :', result_all)

# 通過id查詢
cursor.execute('select amount from fund_info where account='abc@163.com'')
# fetone 仍然返回元組
result_amount = cursor.fetchone()
print(result_amount) # (Decimal('100.00'),)


# 更新 賬號:abc@163.com 的amount值為200.00
cursor.execute('update fund_info set amount=200.00 where account='abc@163.com'')
# 執(zhí)行增 刪 改時 需要commit,不然不會生效
connection.commit()
print('更新成功.')

cursor.execute('delete from fund_info where account='abc@163.com'')
# 執(zhí)行增 刪 改時 需要commit,不然不會生效
connection.commit()
print('刪除成功.')

# 操作完畢之后,必須要關(guān)閉連接
cursor.close()
connection.close()
以上是用PyMySQL操作MySQL的基本知識。這里想再啰嗦一下事務(wù)。

事務(wù)

事務(wù): 事務(wù)指邏輯上的一組操作,組成這組操作的各個單元,要不全部成功,要不全部不成功。
通俗的將:我執(zhí)行多個sql如果其中一個失敗了,那要將之前修改的數(shù)據(jù)進行還原。回滾到執(zhí)行前的狀態(tài)。
拿我們上面的sql來舉例,對于一個賬號 account,資金amount、消費金額consume。我們購物的時候,消費金額增加的同時,錢包里的金額就要對應(yīng)的減少。  這個增加和減少就是一個事務(wù),不可能只增加消費金額,而不減少總金額。
今天只說一下PyMySQL對事務(wù)的處理(如果在MySQL終端進行事務(wù)操作,需要手動開啟 -- start transaction;)

直接看下面的例子

當(dāng)前金額:
mysql> select amount from fund_info where account = 'zxc@163.com';
+--------+
| amount |
+--------+
|  99.00 |
+--------+
1 row in set (0.00 sec)
執(zhí)行消費代碼
import pymysql

connection = pymysql.connect(host='127.0.0.1',  # MySQL服務(wù)器地址
                             port=3306,         # 服務(wù)端口號
                             user='root',       # MySQL用戶名
                             password='root',   # MySQL密碼
                             db='test',         # 要操作的數(shù)據(jù)庫名稱
                             charset='utf8mb4'# 連接編碼
# 創(chuàng)建cursor, 用于執(zhí)行sql
cursor = connection.cursor()

# 代表消費的金額
price = 
15

# 此時賬戶zxc的總金額為99
# 賬戶zxc@163.com 總金額 - price
sql_1 = 
f'update fund_info set amount = amount - {price} where account = 'zxc@163.com''
# 賬戶zxc@163.com 消費金額 + price
sql_2 = 
f'update fund_info set consume = consume + {price} where account = 'zxc@163.com''

try:
    
# 查詢余額是否足夠
    cursor.execute(
'select amount from fund_info where account = 'zxc@163.com'')
    result = cursor.fetchone()
    print(result[
0])
    
# 如果余額不足 拋出異常.
    
if not result or result[0] < price:
        
raise Exception('余額不足...')
    cursor.execute(sql_1)
    print(
'========= 其他業(yè)務(wù)邏輯  執(zhí)行中....')
    cursor.execute(sql_2)
except Exception as e:
    
# 事務(wù)回滾
    connection.rollback()
    print(e)
finally:
    
# 提交sql
    connection.commit()
    
# 關(guān)閉連接
    cursor.close()
    connection.close()
執(zhí)行完畢,查看剩余金額:
mysql> select amount from fund_info where account = 'zxc@163.com';
+--------+
| amount |
+--------+
|  84.00 |
+--------+
1 row in set (0.00 sec)
在代碼中通過raise 模擬異常。演示一下事務(wù)的作用
import pymysql

connection = pymysql.connect(host='127.0.0.1',  # MySQL服務(wù)器地址
                             port=3306,         # 服務(wù)端口號
                             user='root',       # MySQL用戶名
                             password='root',   # MySQL密碼
                             db='test',         # 要操作的數(shù)據(jù)庫名稱
                             charset='utf8mb4'# 連接編碼
# 創(chuàng)建cursor, 用于執(zhí)行sql
cursor = connection.cursor()

# 代表消費的金額
price = 15

# 此時賬戶zxc的總金額為99
# 賬戶zxc@163.com 總金額 - price
sql_1 = f'update fund_info set amount = amount - {price} where account = 'zxc@163.com''
# 賬戶zxc@163.com 消費金額 + price
sql_2 = f'update fund_info set consume = consume + {price} where account = 'zxc@163.com''

try:
    # 查詢余額是否足夠
    cursor.execute('select amount from fund_info where account = 'zxc@163.com'')
    result = cursor.fetchone()
    print(result[0])
    # 如果余額不足 拋出異常.
    if not result or result[0] < price:
        raise Exception('余額不足...')
    cursor.execute(sql_1)
    print('========= 其他業(yè)務(wù)邏輯  執(zhí)行中....')
    raise Exception('模擬業(yè)務(wù)邏輯異常......')
    cursor.execute(sql_2)
except Exception as e:
    print('---- 開始事務(wù)回滾')
    # 事務(wù)回滾
    connection.rollback()
    print(e)
finally:
    # 提交sql
    connection.commit()
    # 關(guān)閉連接
    cursor.close()
    connection.close()
    print('執(zhí)行完畢')

控制條輸出

========= 其他業(yè)務(wù)邏輯  執(zhí)行中....
---- 開始事務(wù)回滾
模擬業(yè)務(wù)邏輯異常......
執(zhí)行完畢

查詢mysql

mysql> select amount from fund_info where account = 'zxc@163.com';
+--------+
| amount |
+--------+
|  84.00 |
+--------+
1 row in set (0.00 sec)
即使執(zhí)行了sql_1(減少總金額的sql語句),金額并沒有被改變。因為在修改consume(執(zhí)行sql_2)之前程序出現(xiàn)了異常,程序就進行了事務(wù)回滾,即:connection.rollback()。所以數(shù)據(jù)被還原到了執(zhí)行事務(wù)之前的樣子。



以上,便是今天的分享,希望大家喜歡,覺得內(nèi)容不錯的,歡迎點擊「在看」支持,謝謝各位。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Python3 MySQL 數(shù)據(jù)庫連接 | 菜鳥教程
Python連接MySQL數(shù)據(jù)庫之pymysql模塊使用
Python+MySQL數(shù)據(jù)庫操作(PyMySQL)
Python操作MySQL數(shù)據(jù)庫
在Python中使用MySQL--PyMySQL的基本使用
mysql與python的交互
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服