帮我用mysql+python写一个图书管理数据库系统
时间: 2023-08-04 10:33:44 浏览: 101
好的,这是一个比较复杂的项目,需要涉及到MySQL数据库的设计和Python编程。以下是一个简单的实现过程示例。
1. 数据库设计
首先,我们需要设计一个数据库,以存储图书信息和借阅记录。以下是一个简单的数据库设计:
- 图书表(Book)
- id: 图书编号
- title: 标题
- author: 作者
- publisher: 出版社
- pub_date: 出版日期
- price: 价格
- total: 总数量
- current: 当前数量
- 借阅记录表(Borrow)
- id: 记录编号
- book_id: 图书编号
- user_id: 用户编号
- borrow_date: 借阅日期
- return_date: 归还日期
2. Python程序实现
接下来,我们可以使用Python编写一个简单的程序,以与数据库交互。
首先,我们需要安装MySQL Python驱动程序。你可以使用以下命令:
```
pip install mysql-connector-python
```
然后,我们需要编写Python程序,以实现以下功能:
- 添加图书信息
- 修改图书信息
- 删除图书信息
- 查询图书信息
- 借阅图书
- 归还图书
- 查询借阅记录
以下是一个简单的Python程序示例:
```python
import mysql.connector
# 连接数据库
db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# 创建游标对象
cursor = db.cursor()
# 添加图书信息
def add_book(title, author, publisher, pub_date, price, total, current):
sql = "INSERT INTO Book (title, author, publisher, pub_date, price, total, current) VALUES (%s, %s, %s, %s, %s, %s, %s)"
val = (title, author, publisher, pub_date, price, total, current)
cursor.execute(sql, val)
db.commit()
# 修改图书信息
def update_book(id, title, author, publisher, pub_date, price, total, current):
sql = "UPDATE Book SET title = %s, author = %s, publisher = %s, pub_date = %s, price = %s, total = %s, current = %s WHERE id = %s"
val = (title, author, publisher, pub_date, price, total, current, id)
cursor.execute(sql, val)
db.commit()
# 删除图书信息
def delete_book(id):
sql = "DELETE FROM Book WHERE id = %s"
val = (id,)
cursor.execute(sql, val)
db.commit()
# 查询图书信息
def search_book(title=None, author=None, publisher=None, pub_date=None):
sql = "SELECT * FROM Book WHERE 1=1"
if title:
sql += " AND title = %s"
val = (title,)
if author:
sql += " AND author = %s"
val = (author,)
if publisher:
sql += " AND publisher = %s"
val = (publisher,)
if pub_date:
sql += " AND pub_date = %s"
val = (pub_date,)
cursor.execute(sql, val)
result = cursor.fetchall()
for row in result:
print(row)
# 借阅图书
def borrow_book(book_id, user_id, borrow_date):
sql = "INSERT INTO Borrow (book_id, user_id, borrow_date) VALUES (%s, %s, %s)"
val = (book_id, user_id, borrow_date)
cursor.execute(sql, val)
db.commit()
sql = "UPDATE Book SET current = current - 1 WHERE id = %s"
val = (book_id,)
cursor.execute(sql, val)
db.commit()
# 归还图书
def return_book(id, return_date):
sql = "UPDATE Borrow SET return_date = %s WHERE id = %s"
val = (return_date, id)
cursor.execute(sql, val)
db.commit()
sql = "SELECT book_id FROM Borrow WHERE id = %s"
val = (id,)
cursor.execute(sql, val)
result = cursor.fetchone()
book_id = result[0]
sql = "UPDATE Book SET current = current + 1 WHERE id = %s"
val = (book_id,)
cursor.execute(sql, val)
db.commit()
# 查询借阅记录
def search_borrow(user_id=None, book_id=None):
sql = "SELECT * FROM Borrow WHERE 1=1"
if user_id:
sql += " AND user_id = %s"
val = (user_id,)
if book_id:
sql += " AND book_id = %s"
val = (book_id,)
cursor.execute(sql, val)
result = cursor.fetchall()
for row in result:
print(row)
# 测试代码
add_book("Python编程从入门到实践", "Eric Matthes", "人民邮电出版社", "2016-11-01", 98.0, 10, 10)
add_book("流浪地球", "刘慈欣", "重庆出版社", "2010-10-01", 42.0, 5, 5)
search_book(author="刘慈欣")
borrow_book(1, 1, "2021-01-01")
search_borrow()
return_book(1, "2021-01-10")
search_borrow()
```
以上代码是一个简单的图书管理系统示例,您可以根据自己的需求进行修改和扩展。
阅读全文