You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pymysql
# 连接数据库 创建连接对象 con
con = pymysql.connect(
host="localhost", # 主机名使用默认即可localhost
user="root", # 用户名一般是root
password="924604223", # 密码,登录数据库的密码,这个是自己设置的
database="atm" # 要连接的数据库
)
print("数据库连接成功!!!")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = con.cursor()
# 创建用户表(users)包含账户account、密码(pwd)、金额money、账户记录record)等
create_table_sql = """
create table if not exists records(
id INT AUTO_INCREMENT PRIMARY KEY,
account VARCHAR(20) ,
time VARCHAR(25) NOT NULL,
content VARCHAR(50),
money float(10,2)
)
"""
# 使用execute()方法执行数据库命令
cursor.execute(create_table_sql)
print("记录表创建成功!!!")
def insert_data(account, time, content,money):
try:
# 插入数据的SQL语句
insert_sql = "INSERT INTO records (account, time,content,money) VALUES (%s, %s, %s, %s)"
data = (account,time, content, money)
# 执行SQL语句
cursor.execute(insert_sql, data)
# 提交事务
con.commit()
except:
print("该账号已存在")
a =[]
def select_data():
# 查询数据的SQL语句
select_sql = "SELECT * FROM records"
# 执行SQL语句
cursor.execute(select_sql)
# 获取查询结果
results = cursor.fetchall()
for row in results:
a.append(list(row))
print(row)
def select_dataA(account):
# 查询数据的SQL语句
select_sql = f"SELECT * FROM records where account = {account}"
# 执行SQL语句
cursor.execute(select_sql)
# 获取查询结果
results = cursor.fetchall()
for row in results:
a.append(list(row))
print(row)
if __name__ == '__main__':
select_data()
print("1001的流水记录如下")
select_dataA('1001')
print("1002的流水记录的类型")