-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql.py
More file actions
59 lines (46 loc) · 1.74 KB
/
mysql.py
File metadata and controls
59 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pymysql
class PayError(Exception):
pass
def pay(from_account, to_account, money):
# 建立数据库连接
db_connection = pymysql.connect(
host="localhost",
port=3306,
user="root",
password="mysqlmysql",
database="test1"
)
# 增删改查的过程
# 它是执行 SQL语句的对象 获取光标对象
db_cursor = db_connection.cursor()
try:
# 检查付款账号是否存在
res1 = db_cursor.execute("select * from bank_account where user_name = %s", from_account)
if res1 == 0:
raise PayError("付款账号不存在")
# 检查余额是否足够
rest_money = db_cursor.fetchone()[2]
if rest_money < money:
raise PayError("账号余额不足")
# 检查转入账号是否存在
res2 = db_cursor.execute("select * from bank_account where user_name = %s", to_account)
if res2 == 0:
raise PayError("转入账号不存在")
sql1 = "update bank_account set account_balance=account_balance-%s where user_name=%s"
db_cursor.execute(sql1, (money, from_account))
sql2 = "update bank_account set account_balance=account_balance+%s where user_name=%s"
db_cursor.execute(sql2, (money, to_account))
# 成功就提交
db_connection.commit()
except Exception as e:
print(e)
print("转账失败")
# 失败就回滚
db_connection.rollback()
finally:
db_cursor.close()
db_connection.close()
from_account = input("请输入需要付款的账号名: ")
to_account = input("请输入需要收款的账号名: ")
money = float(input("请输入需要转账的金额: "))
pay(from_account, to_account, money)