使用Python采用数据库存储数据做一个学校教材管理系统实现增删查改、排序、统计并且交互式界面设计使用Tk直观体现系统功能选择并可循环调用,增删查改各功能代码实现
时间: 2024-01-29 22:02:25 浏览: 38
好的,让我来为你解答这个问题。
首先,我们需要选择一个合适的数据库来存储数据。常用的数据库有MySQL、Oracle、SQL Server等等,选择哪一个可以根据实际情况进行考虑。这里我们以MySQL为例,因为它是一个免费且广泛使用的数据库。
接下来,我们需要安装Python的MySQL驱动程序。可以使用pip命令来安装:
```
pip install mysql-connector-python
```
安装完成后,我们可以开始编写代码实现学校教材管理系统的各项功能:
1. 连接数据库
```python
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
mycursor = mydb.cursor()
```
2. 增加数据
```python
def insert_book(title, author, price):
sql = "INSERT INTO books (title, author, price) VALUES (%s, %s, %s)"
val = (title, author, price)
mycursor.execute(sql, val)
mydb.commit()
print("书籍已添加")
```
3. 删除数据
```python
def delete_book(title):
sql = "DELETE FROM books WHERE title = %s"
val = (title,)
mycursor.execute(sql, val)
mydb.commit()
print("书籍已删除")
```
4. 修改数据
```python
def update_book(title, price):
sql = "UPDATE books SET price = %s WHERE title = %s"
val = (price, title)
mycursor.execute(sql, val)
mydb.commit()
print("书籍已修改")
```
5. 查询数据
```python
def select_book(title):
sql = "SELECT * FROM books WHERE title = %s"
val = (title,)
mycursor.execute(sql, val)
result = mycursor.fetchone()
if result:
print(result)
else:
print("未找到该书籍")
```
6. 排序
```python
def sort_books():
sql = "SELECT * FROM books ORDER BY price ASC"
mycursor.execute(sql)
results = mycursor.fetchall()
for result in results:
print(result)
```
7. 统计
```python
def count_books():
sql = "SELECT COUNT(*) FROM books"
mycursor.execute(sql)
result = mycursor.fetchone()
print("总共有%d本书籍" % result[0])
```
8. 使用Tkinter实现交互式界面设计
```python
import tkinter as tk
def add_book():
title = e1.get()
author = e2.get()
price = e3.get()
insert_book(title, author, price)
def delete_book():
title = e1.get()
delete_book(title)
def update_book():
title = e1.get()
price = e3.get()
update_book(title, price)
def select_book():
title = e1.get()
select_book(title)
def sort_books():
sort_books()
def count_books():
count_books()
def exit_program():
root.destroy()
root = tk.Tk()
root.title("学校教材管理系统")
root.geometry("400x300")
tk.Label(root, text="书籍名称").grid(row=0, column=0)
tk.Label(root, text="作者").grid(row=1, column=0)
tk.Label(root, text="价格").grid(row=2, column=0)
e1 = tk.Entry(root)
e2 = tk.Entry(root)
e3 = tk.Entry(root)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
tk.Button(root, text="添加书籍", command=add_book).grid(row=3, column=0)
tk.Button(root, text="删除书籍", command=delete_book).grid(row=3, column=1)
tk.Button(root, text="修改书籍", command=update_book).grid(row=3, column=2)
tk.Button(root, text="查询书籍", command=select_book).grid(row=4, column=0)
tk.Button(root, text="排序书籍", command=sort_books).grid(row=4, column=1)
tk.Button(root, text="统计书籍", command=count_books).grid(row=4, column=2)
tk.Button(root, text="退出程序", command=exit_program).grid(row=5, column=1)
root.mainloop()
```
以上代码可以实现一个简单的学校教材管理系统,包括增删查改、排序和统计功能,并且提供了基于Tk的交互式界面,方便用户进行操作。当然,这只是一个示例,实际应用中可能需要更多的功能和更复杂的逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)