写一个简单的图书管理系统
时间: 2023-12-06 21:03:57 浏览: 44
好的,下面是一个简单的图书管理系统的设计:
1. 数据库设计
设计一张名为Books的表,包含以下字段:
- id:图书ID,自增长整数类型
- name:图书名称,字符串类型
- author:图书作者,字符串类型
- publish_date:出版日期,日期类型
- price:价格,浮点数类型
- count:库存数量,整数类型
2. 界面设计
系统主界面包含以下功能:
- 查询图书:可以根据图书名称和作者进行查询
- 添加图书:可以添加新的图书信息
- 修改图书:可以修改图书的名称、作者、出版日期、价格和库存数量
- 删除图书:可以删除指定的图书信息
3. 功能实现
使用Python语言和MySQL数据库进行开发,使用PyQt5库实现界面设计。
首先要连接数据库:
```python
import pymysql
# 连接数据库
conn = pymysql.connect(host='localhost', user='root', password='123456', database='books', charset='utf8')
cursor = conn.cursor()
```
查询图书:
```python
def search_book(self):
# 获取输入的图书名称和作者
name = self.name_input.text()
author = self.author_input.text()
# 查询语句
sql = "SELECT * FROM Books WHERE name LIKE '%{}%' AND author LIKE '%{}%'".format(name, author)
# 执行查询
cursor.execute(sql)
results = cursor.fetchall()
# 显示查询结果
self.tableWidget.setRowCount(len(results))
for i, row in enumerate(results):
for j, col in enumerate(row):
item = QtWidgets.QTableWidgetItem(str(col))
self.tableWidget.setItem(i, j, item)
```
添加图书:
```python
def add_book(self):
# 获取输入的图书信息
name = self.name_input.text()
author = self.author_input.text()
publish_date = self.date_input.text()
price = self.price_input.text()
count = self.count_input.text()
# 插入语句
sql = "INSERT INTO Books (name, author, publish_date, price, count) VALUES ('{}', '{}', '{}', '{}', '{}')".format(name, author, publish_date, price, count)
# 执行插入
cursor.execute(sql)
conn.commit()
# 清空输入框
self.name_input.clear()
self.author_input.clear()
self.date_input.clear()
self.price_input.clear()
self.count_input.clear()
# 刷新表格显示
self.search_book()
```
修改图书:
```python
def modify_book(self):
# 获取当前选中的行号
row = self.tableWidget.currentRow()
# 获取需要修改的图书信息
id = self.tableWidget.item(row, 0).text()
name = self.name_input.text()
author = self.author_input.text()
publish_date = self.date_input.text()
price = self.price_input.text()
count = self.count_input.text()
# 更新语句
sql = "UPDATE Books SET name='{}', author='{}', publish_date='{}', price='{}', count='{}' WHERE id={}".format(name, author, publish_date, price, count, id)
# 执行更新
cursor.execute(sql)
conn.commit()
# 清空输入框
self.name_input.clear()
self.author_input.clear()
self.date_input.clear()
self.price_input.clear()
self.count_input.clear()
# 刷新表格显示
self.search_book()
```
删除图书:
```python
def delete_book(self):
# 获取当前选中的行号
row = self.tableWidget.currentRow()
# 获取需要删除的图书ID
id = self.tableWidget.item(row, 0).text()
# 删除语句
sql = "DELETE FROM Books WHERE id={}".format(id)
# 执行删除
cursor.execute(sql)
conn.commit()
# 刷新表格显示
self.search_book()
```
完整代码可以参考下面的链接:
https://github.com/Minions1128/BookManagementSystem