图书管理系统只使用mysql完整代码
时间: 2023-09-16 22:14:11 浏览: 79
SQL数据库图书管理系统(完整代码)
5星 · 资源好评率100%
以下是一个使用 Python 和 MySQL 的完整图书管理系统示例代码。请注意,这只是一个简单的示例,实际应用中还需要更多的功能和安全性措施。
```python
import mysql.connector
from datetime import datetime, timedelta
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="library"
)
# 创建游标
mycursor = mydb.cursor()
# 创建图书信息表
mycursor.execute("CREATE TABLE IF NOT EXISTS books (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), author VARCHAR(255), publisher VARCHAR(255), publish_date DATE, price FLOAT)")
# 创建读者信息表
mycursor.execute("CREATE TABLE IF NOT EXISTS readers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), gender VARCHAR(10), birthday DATE, contact VARCHAR(255))")
# 创建借阅记录表
mycursor.execute("CREATE TABLE IF NOT EXISTS borrows (id INT AUTO_INCREMENT PRIMARY KEY, book_id INT, reader_id INT, borrow_date DATE, due_date DATE, return_date DATE)")
# 添加图书信息
def add_book(title, author, publisher, publish_date, price):
sql = "INSERT INTO books (title, author, publisher, publish_date, price) VALUES (%s, %s, %s, %s, %s)"
val = (title, author, publisher, publish_date, price)
mycursor.execute(sql, val)
mydb.commit()
print("添加图书成功!")
# 添加读者信息
def add_reader(name, gender, birthday, contact):
sql = "INSERT INTO readers (name, gender, birthday, contact) VALUES (%s, %s, %s, %s)"
val = (name, gender, birthday, contact)
mycursor.execute(sql, val)
mydb.commit()
print("添加读者成功!")
# 借阅图书
def borrow_book(book_id, reader_id, borrow_date, due_days):
due_date = borrow_date + timedelta(days=due_days)
sql = "INSERT INTO borrows (book_id, reader_id, borrow_date, due_date) VALUES (%s, %s, %s, %s)"
val = (book_id, reader_id, borrow_date, due_date)
mycursor.execute(sql, val)
mydb.commit()
print("借阅图书成功!")
# 归还图书
def return_book(borrow_id, return_date):
sql = "UPDATE borrows SET return_date = %s WHERE id = %s"
val = (return_date, borrow_id)
mycursor.execute(sql, val)
mydb.commit()
print("归还图书成功!")
# 查询图书信息
def search_books(keyword):
sql = "SELECT * FROM books WHERE title LIKE %s OR author LIKE %s"
val = ("%" + keyword + "%", "%" + keyword + "%")
mycursor.execute(sql, val)
results = mycursor.fetchall()
for result in results:
print(result)
# 查询读者信息
def search_readers(keyword):
sql = "SELECT * FROM readers WHERE name LIKE %s OR contact LIKE %s"
val = ("%" + keyword + "%", "%" + keyword + "%")
mycursor.execute(sql, val)
results = mycursor.fetchall()
for result in results:
print(result)
# 查询借阅记录
def search_borrows(keyword):
sql = "SELECT books.title, readers.name, borrows.borrow_date, borrows.due_date, borrows.return_date FROM borrows INNER JOIN books ON borrows.book_id = books.id INNER JOIN readers ON borrows.reader_id = readers.id WHERE books.title LIKE %s OR readers.name LIKE %s"
val = ("%" + keyword + "%", "%" + keyword + "%")
mycursor.execute(sql, val)
results = mycursor.fetchall()
for result in results:
print(result)
# 测试
add_book("Python入门", "张三", "清华大学出版社", datetime(2020, 1, 1), 39.9)
add_reader("李四", "男", datetime(1990, 1, 1), "13888888888")
borrow_book(1, 1, datetime(2021, 1, 1), 30)
return_book(1, datetime(2021, 1, 31))
search_books("Python")
search_readers("李四")
search_borrows("Python")
```
这个示例代码实现了添加图书、添加读者、借阅图书、归还图书、查询图书信息、查询读者信息和查询借阅记录等功能。在实际应用中,还需要考虑一些安全性措施,例如身份验证、权限控制等。
阅读全文