用Python编写用sqlite数据库设计学生一卡通数据类型学生一卡通的基本数据成员至少包括:卡号(学号), 姓名, 性别, 余额,消费总额 (食堂、机房),图书借用信息和还书信息。系统采用文字界面控制各功能,基本功能包括,一卡通的充值、消费(判断一卡通里面的钱是否够消费)、余额查询,图书的借书和还书,最后可以按照按性别、班级进行消费总额统计,通过可视化方式比较两者的消费总额
时间: 2024-03-15 17:45:37 浏览: 94
Python解析excel文件存入sqlite数据库的方法
好的,我可以帮你解答这个问题。首先,我们需要安装Python中的sqlite3库,以便操作sqlite数据库。在安装完sqlite3库之后,我们可以使用以下代码创建一个名为"student_card.db"的sqlite数据库,并在其中创建一个名为"student_card"的表,用于存储学生一卡通的基本信息。
```python
import sqlite3
# 创建sqlite数据库连接
conn = sqlite3.connect('student_card.db')
# 创建游标
cursor = conn.cursor()
# 创建student_card表
cursor.execute('''
CREATE TABLE student_card (
id INTEGER PRIMARY KEY AUTOINCREMENT,
card_number VARCHAR(20) NOT NULL,
name VARCHAR(20) NOT NULL,
gender VARCHAR(10) NOT NULL,
balance FLOAT NOT NULL,
canteen_consumption FLOAT DEFAULT 0,
computer_room_consumption FLOAT DEFAULT 0,
book_borrowed INTEGER DEFAULT 0,
book_returned INTEGER DEFAULT 0
)
''')
# 提交事务
conn.commit()
# 关闭连接
conn.close()
```
上述代码创建了一个名为"student_card"的表,其中包含了学生一卡通的基本信息,包括卡号、姓名、性别、余额、食堂消费总额、机房消费总额、借书信息和还书信息。
接下来,我们可以编写一些Python函数来实现一卡通的充值、消费、余额查询、图书的借还等功能。这些函数的实现过程中,我们需要使用sqlite3库进行数据库的操作,如插入数据、更新数据、查询数据等。
```
# 充值
def recharge(card_number, amount):
conn = sqlite3.connect('student_card.db')
cursor = conn.cursor()
# 查询当前余额
cursor.execute('SELECT balance FROM student_card WHERE card_number = ?', (card_number,))
result = cursor.fetchone()
balance = result[0]
# 更新余额
new_balance = balance + amount
cursor.execute('UPDATE student_card SET balance = ? WHERE card_number = ?', (new_balance, card_number))
conn.commit()
# 输出充值成功信息
print('充值成功!卡号为{}的一卡通余额为{}'.format(card_number, new_balance))
conn.close()
# 消费
def consume(card_number, amount, consumption_type):
conn = sqlite3.connect('student_card.db')
cursor = conn.cursor()
# 查询当前余额
cursor.execute('SELECT balance FROM student_card WHERE card_number = ?', (card_number,))
result = cursor.fetchone()
balance = result[0]
# 判断余额是否充足
if balance < amount:
print('余额不足,无法完成消费!')
return
# 更新余额和消费总额
new_balance = balance - amount
if consumption_type == 'canteen':
cursor.execute('UPDATE student_card SET balance = ?, canteen_consumption = canteen_consumption + ? WHERE card_number = ?', (new_balance, amount, card_number))
elif consumption_type == 'computer_room':
cursor.execute('UPDATE student_card SET balance = ?, computer_room_consumption = computer_room_consumption + ? WHERE card_number = ?', (new_balance, amount, card_number))
conn.commit()
# 输出消费成功信息
print('消费成功!卡号为{}的一卡通余额为{}'.format(card_number, new_balance))
conn.close()
# 余额查询
def check_balance(card_number):
conn = sqlite3.connect('student_card.db')
cursor = conn.cursor()
# 查询当前余额
cursor.execute('SELECT balance FROM student_card WHERE card_number = ?', (card_number,))
result = cursor.fetchone()
balance = result[0]
# 输出余额信息
print('卡号为{}的一卡通余额为{}'.format(card_number, balance))
conn.close()
# 借书
def borrow_book(card_number):
conn = sqlite3.connect('student_card.db')
cursor = conn.cursor()
# 更新借书信息
cursor.execute('UPDATE student_card SET book_borrowed = book_borrowed + 1 WHERE card_number = ?', (card_number,))
conn.commit()
# 输出借书信息
print('卡号为{}的学生已成功借书'.format(card_number))
conn.close()
# 还书
def return_book(card_number):
conn = sqlite3.connect('student_card.db')
cursor = conn.cursor()
# 更新还书信息
cursor.execute('UPDATE student_card SET book_returned = book_returned + 1 WHERE card_number = ?', (card_number,))
conn.commit()
# 输出还书信息
print('卡号为{}的学生已成功还书'.format(card_number))
conn.close()
# 按性别统计消费总额
def gender_consumption_statistics():
conn = sqlite3.connect('student_card.db')
cursor = conn.cursor()
# 查询男生和女生的消费总额
cursor.execute('SELECT gender, SUM(canteen_consumption + computer_room_consumption) FROM student_card GROUP BY gender')
result = cursor.fetchall()
# 输出消费总额统计信息
print('性别\t消费总额')
for row in result:
print('{}\t{}'.format(row[0], row[1]))
conn.close()
# 按班级统计消费总额
def class_consumption_statistics():
conn = sqlite3.connect('student_card.db')
cursor = conn.cursor()
# 查询各班级的消费总额
cursor.execute('SELECT SUBSTR(card_number, 1, 2), SUM(canteen_consumption + computer_room_consumption) FROM student_card GROUP BY SUBSTR(card_number, 1, 2)')
result = cursor.fetchall()
# 输出消费总额统计信息
print('班级\t消费总额')
for row in result:
print('{}\t{}'.format(row[0], row[1]))
conn.close()
```
最后,我们可以编写一些文字界面的控制代码,根据用户的输入调用相应的函数,实现一卡通系统的各种功能。
```
while True:
print('1. 充值')
print('2. 消费')
print('3. 余额查询')
print('4. 借书')
print('5. 还书')
print('6. 按性别统计消费总额')
print('7. 按班级统计消费总额')
print('0. 退出')
choice = input('请选择功能:')
if choice == '1':
card_number = input('请输入卡号:')
amount = float(input('请输入充值金额:'))
recharge(card_number, amount)
elif choice == '2':
card_number = input('请输入卡号:')
amount = float(input('请输入消费金额:'))
consumption_type = input('请输入消费类型(canteen或computer_room):')
consume(card_number, amount, consumption_type)
elif choice == '3':
card_number = input('请输入卡号:')
check_balance(card_number)
elif choice == '4':
card_number = input('请输入卡号:')
borrow_book(card_number)
elif choice == '5':
card_number = input('请输入卡号:')
return_book(card_number)
elif choice == '6':
gender_consumption_statistics()
elif choice == '7':
class_consumption_statistics()
elif choice == '0':
break
else:
print('输入有误,请重新输入!')
```
通过以上代码,我们就可以实现一个基本的学生一卡通系统,支持充值、消费、余额查询、图书的借还和消费总额统计等功能。在实际应用中,我们可以根据需要进行修改和扩展,使其更符合实际需求。
阅读全文