用Python编写用sqlite数据库设计学生一卡通数据类型学生一卡通的基本数据成员至少包括:卡号(学号), 姓名, 性别, 余额,消费总额 (食堂、机房),图书借阅信息。系统采用gul界面,基本功能包括,一卡通的充值、消费(判断一卡通里面的钱是否够消费)、余额查询,图书的借书和还书,最后可以按照按性别、班级进行消费总额统计,通过可视化方式比较两者的消费总额
时间: 2024-03-15 11:45:23 浏览: 82
好的,我可以为你提供一些关于用Python和SQLite数据库设计学生一卡通管理系统的代码示例。但是由于篇幅限制,我无法提供完整的程序,只能给出关键代码和思路供你参考。
首先,你需要安装Python和SQLite3模块。安装好后,你可以创建一个Python脚本,命名为`student_card.py`,在脚本中实现相关功能。
1. 创建数据库和表格
你可以使用`sqlite3`模块创建数据库和表格。下面是创建`student_card`表格的代码示例:
```python
import sqlite3
# 连接数据库
conn = sqlite3.connect('student_card.db')
# 创建表格
conn.execute('''
CREATE TABLE student_card (
card_num TEXT PRIMARY KEY,
name TEXT,
gender TEXT,
balance FLOAT,
total_consumption_canteen FLOAT,
total_consumption_computer_room FLOAT,
total_consumption_book FLOAT
)
''')
# 关闭数据库连接
conn.close()
```
2. 实现充值和消费功能
在GUI界面中,你可以使用`tkinter`模块创建相关的控件,比如`Entry`、`Button`等。下面是实现充值和消费功能的代码示例:
```python
import sqlite3
import tkinter as tk
# 连接数据库
conn = sqlite3.connect('student_card.db')
# 充值功能
def recharge():
card_num = card_entry.get()
recharge_amount = float(amount_entry.get())
cursor = conn.cursor()
cursor.execute('SELECT * FROM student_card WHERE card_num = ?', [card_num])
row = cursor.fetchone()
if row is None:
tk.messagebox.showerror('错误', '该学生卡号不存在')
else:
balance = row[3]
new_balance = balance + recharge_amount
cursor.execute('UPDATE student_card SET balance = ? WHERE card_num = ?', [new_balance, card_num])
conn.commit()
tk.messagebox.showinfo('提示', '充值成功')
# 消费功能
def consume():
card_num = card_entry.get()
consumption_amount = float(amount_entry.get())
cursor = conn.cursor()
cursor.execute('SELECT * FROM student_card WHERE card_num = ?', [card_num])
row = cursor.fetchone()
if row is None:
tk.messagebox.showerror('错误', '该学生卡号不存在')
else:
balance = row[3]
if balance < consumption_amount:
tk.messagebox.showerror('错误', '余额不足')
else:
new_balance = balance - consumption_amount
total_consumption_canteen = row[4]
total_consumption_computer_room = row[5]
total_consumption_book = row[6]
if consumption_type.get() == '食堂':
total_consumption_canteen += consumption_amount
elif consumption_type.get() == '机房':
total_consumption_computer_room += consumption_amount
elif consumption_type.get() == '图书':
total_consumption_book += consumption_amount
cursor.execute('UPDATE student_card SET balance = ?, total_consumption_canteen = ?, total_consumption_computer_room = ?, total_consumption_book = ? WHERE card_num = ?', [new_balance, total_consumption_canteen, total_consumption_computer_room, total_consumption_book, card_num])
conn.commit()
tk.messagebox.showinfo('提示', '消费成功')
# 创建GUI界面
root = tk.Tk()
# 创建控件
card_label = tk.Label(root, text='学生卡号')
card_entry = tk.Entry(root)
amount_label = tk.Label(root, text='金额')
amount_entry = tk.Entry(root)
consumption_type_label = tk.Label(root, text='消费类型')
consumption_type = tk.StringVar()
canteen_radio = tk.Radiobutton(root, text='食堂', variable=consumption_type, value='食堂')
computer_room_radio = tk.Radiobutton(root, text='机房', variable=consumption_type, value='机房')
book_radio = tk.Radiobutton(root, text='图书', variable=consumption_type, value='图书')
recharge_button = tk.Button(root, text='充值', command=recharge)
consume_button = tk.Button(root, text='消费', command=consume)
# 布局控件
card_label.grid(row=0, column=0)
card_entry.grid(row=0, column=1)
amount_label.grid(row=1, column=0)
amount_entry.grid(row=1, column=1)
consumption_type_label.grid(row=2, column=0)
canteen_radio.grid(row=2, column=1)
computer_room_radio.grid(row=2, column=2)
book_radio.grid(row=2, column=3)
recharge_button.grid(row=3, column=0)
consume_button.grid(row=3, column=1)
# 进入主循环
root.mainloop()
# 关闭数据库连接
conn.close()
```
3. 实现余额查询功能
余额查询功能比较简单,可以直接从数据库中查询对应学生卡号的余额。下面是代码示例:
```python
import sqlite3
import tkinter as tk
# 连接数据库
conn = sqlite3.connect('student_card.db')
# 余额查询功能
def query_balance():
card_num = card_entry.get()
cursor = conn.cursor()
cursor.execute('SELECT balance FROM student_card WHERE card_num = ?', [card_num])
row = cursor.fetchone()
if row is None:
tk.messagebox.showerror('错误', '该学生卡号不存在')
else:
balance = row[0]
tk.messagebox.showinfo('余额', '当前余额为:{}'.format(balance))
# 创建GUI界面
root = tk.Tk()
# 创建控件
card_label = tk.Label(root, text='学生卡号')
card_entry = tk.Entry(root)
query_button = tk.Button(root, text='查询余额', command=query_balance)
# 布局控件
card_label.grid(row=0, column=0)
card_entry.grid(row=0, column=1)
query_button.grid(row=1, column=0)
# 进入主循环
root.mainloop()
# 关闭数据库连接
conn.close()
```
4. 实现图书借阅和还书功能
图书借阅和还书功能需要在数据库中添加或删除对应的借阅信息。下面是代码示例:
```python
import sqlite3
import tkinter as tk
# 连接数据库
conn = sqlite3.connect('student_card.db')
# 图书借阅功能
def borrow_book():
card_num = card_entry.get()
book_name = book_entry.get()
cursor = conn.cursor()
cursor.execute('SELECT * FROM student_card WHERE card_num = ?', [card_num])
row = cursor.fetchone()
if row is None:
tk.messagebox.showerror('错误', '该学生卡号不存在')
else:
cursor.execute('INSERT INTO book_borrow (card_num, book_name) VALUES (?, ?)', [card_num, book_name])
conn.commit()
tk.messagebox.showinfo('提示', '借书成功')
# 图书还书功能
def return_book():
card_num = card_entry.get()
book_name = book_entry.get()
cursor = conn.cursor()
cursor.execute('SELECT * FROM book_borrow WHERE card_num = ? AND book_name = ?', [card_num, book_name])
row = cursor.fetchone()
if row is None:
tk.messagebox.showerror('错误', '该学生未借阅该书籍')
else:
cursor.execute('DELETE FROM book_borrow WHERE card_num = ? AND book_name = ?', [card_num, book_name])
conn.commit()
tk.messagebox.showinfo('提示', '还书成功')
# 创建GUI界面
root = tk.Tk()
# 创建控件
card_label = tk.Label(root, text='学生卡号')
card_entry = tk.Entry(root)
book_label = tk.Label(root, text='书名')
book_entry = tk.Entry(root)
borrow_button = tk.Button(root, text='借书', command=borrow_book)
return_button = tk.Button(root, text='还书', command=return_book)
# 布局控件
card_label.grid(row=0, column=0)
card_entry.grid(row=0, column=1)
book_label.grid(row=1, column=0)
book_entry.grid(row=1, column=1)
borrow_button.grid(row=2, column=0)
return_button.grid(row=2, column=1)
# 进入主循环
root.mainloop()
# 关闭数据库连接
conn.close()
```
5. 实现按性别、班级统计消费总额并可视化展示
按性别、班级统计消费总额需要先从数据库中查询对应的数据,然后进行统计并绘制可视化图表。下面是代码示例:
```python
import sqlite3
import matplotlib.pyplot as plt
# 连接数据库
conn = sqlite3.connect('student_card.db')
# 获取按性别统计的数据
def get_consumption_by_gender():
cursor = conn.cursor()
cursor.execute('SELECT gender, SUM(total_consumption_canteen + total_consumption_computer_room + total_consumption_book) FROM student_card GROUP BY gender')
rows = cursor.fetchall()
return rows
# 获取按班级统计的数据
def get_consumption_by_class():
cursor = conn.cursor()
cursor.execute('SELECT SUBSTR(card_num, 1, 4) AS class_num, SUM(total_consumption_canteen + total_consumption_computer_room + total_consumption_book) FROM student_card GROUP BY class_num')
rows = cursor.fetchall()
return rows
# 统计消费总额并绘制可视化图表
def draw_consumption_chart():
if chart_type.get() == '按性别统计':
data = get_consumption_by_gender()
labels = ['男', '女']
elif chart_type.get() == '按班级统计':
data = get_consumption_by_class()
labels = [row[0] for row in data]
values = [row[1] for row in data]
plt.bar(labels, values)
plt.show()
# 创建GUI界面
root = tk.Tk()
# 创建控件
chart_type_label = tk.Label(root, text='统计类型')
chart_type = tk.StringVar()
gender_radio = tk.Radiobutton(root, text='按性别统计', variable=chart_type, value='按性别统计')
class_radio = tk.Radiobutton(root, text='按班级统计', variable=chart_type, value='按班级统计')
draw_button = tk.Button(root, text='绘制图表', command=draw_consumption_chart)
# 布局控件
chart_type_label.grid(row=0, column=0)
gender_radio.grid(row=0, column=1)
class_radio.grid(row=0, column=2)
draw_button.grid(row=1, column=0)
# 进入主循环
root.mainloop()
# 关闭数据库连接
conn.close()
```
以上就是一个基于Python和SQLite数据库的学生一卡通管理系统的代码实现思路和示例。希望对你有所帮助。
阅读全文