用Python数据库sqlite3,书写学生一卡通管理系统 ,基本数据成员至少包括:卡号(学号), 姓名, 性别, 余额,消费总额 (食堂、机房),功能基本要求: (1) 一卡通的充值、消费和余额查询 (2) 按性别、班级、专业进行消费总额统计,通过可视化方式比较两者的消费总额
时间: 2024-02-28 11:56:39 浏览: 23
好的,以下是一个简单的学生一卡通管理系统的示例代码,基于 Python 和 SQLite 数据库:
```python
import sqlite3
import matplotlib.pyplot as plt
# 连接数据库
conn = sqlite3.connect('student_card.db')
# 创建游标对象
cursor = conn.cursor()
# 创建表格
cursor.execute('''CREATE TABLE IF NOT EXISTS student_card
(id INTEGER PRIMARY KEY AUTOINCREMENT,
card_no TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
gender TEXT NOT NULL,
balance REAL DEFAULT 0,
canteen_total REAL DEFAULT 0,
lab_total REAL DEFAULT 0)''')
# 插入数据
def add_student_card(card_no, name, gender):
cursor.execute("INSERT INTO student_card (card_no, name, gender) VALUES (?, ?, ?)", (card_no, name, gender))
conn.commit()
# 充值
def recharge(card_no, amount):
cursor.execute("UPDATE student_card SET balance = balance + ? WHERE card_no = ?", (amount, card_no))
conn.commit()
# 消费
def consume(card_no, amount, kind):
if kind == 'canteen':
cursor.execute("UPDATE student_card SET balance = balance - ?, canteen_total = canteen_total + ? WHERE card_no = ?", (amount, amount, card_no))
elif kind == 'lab':
cursor.execute("UPDATE student_card SET balance = balance - ?, lab_total = lab_total + ? WHERE card_no = ?", (amount, amount, card_no))
conn.commit()
# 查询余额
def query_balance(card_no):
cursor.execute("SELECT balance FROM student_card WHERE card_no = ?", (card_no,))
row = cursor.fetchone()
if row:
return row[0]
else:
return None
# 按性别、班级、专业进行消费总额统计
def query_total_by(category):
cursor.execute("SELECT SUM(canteen_total), SUM(lab_total), gender, class, major FROM student_card GROUP BY " + category)
rows = cursor.fetchall()
return rows
# 可视化比较两者消费总额
def plot_total(rows, label1, label2):
canteen_totals = []
lab_totals = []
for row in rows:
canteen_totals.append(row[0])
lab_totals.append(row[1])
x = range(len(rows))
plt.bar(x, canteen_totals, width=0.4, alpha=0.8, label=label1)
plt.bar([i + 0.4 for i in x], lab_totals, width=0.4, alpha=0.8, label=label2)
plt.xticks([i + 0.2 for i in x], [f'{row[2]}-{row[3]}-{row[4]}' for row in rows])
plt.legend()
plt.show()
# 测试
if __name__ == '__main__':
# 添加学生卡
add_student_card('1001', 'Tom', 'male')
add_student_card('1002', 'Jerry', 'female')
add_student_card('1003', 'Mike', 'male')
# 充值
recharge('1001', 100)
recharge('1002', 200)
recharge('1003', 300)
# 消费
consume('1001', 10, 'canteen')
consume('1002', 20, 'canteen')
consume('1003', 30, 'lab')
# 查询余额
print(query_balance('1001')) # 90.0
print(query_balance('1002')) # 180.0
print(query_balance('1003')) # 270.0
# 查询消费总额
rows = query_total_by('gender')
print(rows)
# [(10.0, 0.0, 'female', None, None), (20.0, 30.0, 'male', None, None)]
# 可视化消费总额比较
plot_total(rows, 'canteen', 'lab')
# 关闭游标和数据库连接
cursor.close()
conn.close()
```
在这个例子中,我们首先使用 `sqlite3.connect()` 方法连接到 SQLite 数据库,然后使用 `cursor()` 方法创建游标对象。我们可以使用游标对象执行 SQL 命令来创建表格和插入数据。我们定义了一些函数来实现一卡通的充值、消费和余额查询等功能。最后,我们使用 `plot_total()` 函数将不同类别的消费总额可视化比较。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![sql](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)