用Python数据库sqlite3,书写学生一卡通管理系统 ,基本数据成员至少包括:卡号(学号), 姓名, 性别, 余额,消费总额 (食堂、机房),功能基本要求:(1)数据让用户输入 (2) 一卡通的充值、消费和余额查询 (3) 按性别、班级、专业对消费总额进行排序
时间: 2024-02-28 18:56:52 浏览: 18
好的,这是一个比较全面的学生一卡通管理系统,需要用到Python的sqlite3库来实现。首先,我们需要创建一个数据库并连接:
```python
import sqlite3
conn = sqlite3.connect('student_card.db')
c = conn.cursor()
```
然后,我们需要创建一个表来存储学生的信息:
```python
c.execute('''CREATE TABLE student
(id INTEGER PRIMARY KEY AUTOINCREMENT,
card_number TEXT,
name TEXT,
gender TEXT,
balance REAL,
canteen_total REAL,
computer_room_total REAL)''')
```
这个表包括了学生的卡号、姓名、性别、余额、食堂消费总额和机房消费总额。
接下来,我们需要实现一些基本功能:
1. 数据让用户输入
```python
def input_student_info():
card_number = input("请输入学生一卡通卡号:")
name = input("请输入学生姓名:")
gender = input("请输入学生性别:")
balance = float(input("请输入学生余额:"))
canteen_total = float(input("请输入学生食堂消费总额:"))
computer_room_total = float(input("请输入学生机房消费总额:"))
c.execute("INSERT INTO student (card_number, name, gender, balance, canteen_total, computer_room_total) VALUES (?, ?, ?, ?, ?, ?)",
(card_number, name, gender, balance, canteen_total, computer_room_total))
conn.commit()
```
这个函数会让用户输入学生的信息,并将信息插入到数据库中。
2. 充值
```python
def recharge():
card_number = input("请输入学生一卡通卡号:")
amount = float(input("请输入充值金额:"))
c.execute("SELECT balance FROM student WHERE card_number=?", (card_number,))
balance = c.fetchone()[0]
new_balance = balance + amount
c.execute("UPDATE student SET balance=? WHERE card_number=?", (new_balance, card_number))
conn.commit()
```
这个函数会让用户输入卡号和充值金额,然后根据卡号查询到当前余额,将充值金额加上去更新余额。
3. 消费
```python
def consume():
card_number = input("请输入学生一卡通卡号:")
amount = float(input("请输入消费金额:"))
category = input("请输入消费类别(canteen或computer_room):")
c.execute("SELECT balance, {}_total FROM student WHERE card_number=?".format(category), (card_number,))
result = c.fetchone()
balance = result[0]
total = result[1]
new_balance = balance - amount
new_total = total + amount
c.execute("UPDATE student SET balance=?, {}_total=? WHERE card_number=?".format(category), (new_balance, new_total, card_number))
conn.commit()
```
这个函数会让用户输入卡号、消费金额和消费类别(食堂或机房),然后根据卡号和消费类别查询到当前余额和消费总额,将消费金额从余额中扣除,并将消费金额加上去更新总额。
4. 余额查询
```python
def check_balance():
card_number = input("请输入学生一卡通卡号:")
c.execute("SELECT balance FROM student WHERE card_number=?", (card_number,))
balance = c.fetchone()[0]
print("当前余额为:{}".format(balance))
```
这个函数会让用户输入卡号,然后查询到当前余额,并打印出来。
5. 按性别、班级、专业对消费总额进行排序
```python
def total_by_category():
category = input("请输入消费类别(canteen或computer_room):")
c.execute("SELECT gender, SUM({}_total) FROM student GROUP BY gender ORDER BY SUM({}_total) DESC".format(category, category))
result = c.fetchall()
for row in result:
print("性别:{},消费总额:{}".format(row[0], row[1]))
```
这个函数会让用户输入消费类别(食堂或机房),然后按性别进行分组,计算每组的消费总额,并按消费总额倒序排列,最后打印出结果。
以上就是一个基本的学生一卡通管理系统的实现。完整代码如下: