使用MySQL和python实现图书信息管理系统的设计
时间: 2024-05-14 17:18:22 浏览: 100
图书信息管理系统主要包括以下模块:图书信息管理、借阅管理、归还管理、罚款管理等。下面是使用MySQL和Python实现图书信息管理系统的设计步骤:
1. 数据库设计
首先需要设计数据库,包括图书信息表、读者信息表、借阅信息表、罚款信息表等。具体字段可以根据需求自行设计。以下是一个简单的数据库设计示例:
```
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`author` varchar(100) NOT NULL,
`publisher` varchar(100) NOT NULL,
`isbn` varchar(100) NOT NULL,
`price` float NOT NULL,
`total` int(11) NOT NULL,
`stock` int(11) NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `reader` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`gender` varchar(10) NOT NULL,
`tel` varchar(20) NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `borrow` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`book_id` int(11) NOT NULL,
`reader_id` int(11) NOT NULL,
`borrow_date` datetime NOT NULL,
`return_date` datetime NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `book_id` (`book_id`),
KEY `reader_id` (`reader_id`),
CONSTRAINT `borrow_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `book` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `borrow_ibfk_2` FOREIGN KEY (`reader_id`) REFERENCES `reader` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `fine` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`borrow_id` int(11) NOT NULL,
`amount` float NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `borrow_id` (`borrow_id`),
CONSTRAINT `fine_ibfk_1` FOREIGN KEY (`borrow_id`) REFERENCES `borrow` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. Python代码实现
接下来,使用Python连接MySQL数据库,实现对数据库的增删改查操作。以下是一个简单的Python代码实现示例:
```
import pymysql
class Book:
def __init__(self, id, name, author, publisher, isbn, price, total, stock, created_at):
self.id = id
self.name = name
self.author = author
self.publisher = publisher
self.isbn = isbn
self.price = price
self.total = total
self.stock = stock
self.created_at = created_at
class Reader:
def __init__(self, id, name, gender, tel, created_at):
self.id = id
self.name = name
self.gender = gender
self.tel = tel
self.created_at = created_at
class Borrow:
def __init__(self, id, book_id, reader_id, borrow_date, return_date, created_at):
self.id = id
self.book_id = book_id
self.reader_id = reader_id
self.borrow_date = borrow_date
self.return_date = return_date
self.created_at = created_at
class Fine:
def __init__(self, id, borrow_id, amount, created_at):
self.id = id
self.borrow_id = borrow_id
self.amount = amount
self.created_at = created_at
class Library:
def __init__(self):
self.conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='library', charset='utf8mb4')
self.cursor = self.conn.cursor()
def insert_book(self, book):
sql = "INSERT INTO book (name, author, publisher, isbn, price, total, stock, created_at) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
self.cursor.execute(sql, (book.name, book.author, book.publisher, book.isbn, book.price, book.total, book.stock, book.created_at))
self.conn.commit()
def update_book(self, book):
sql = "UPDATE book SET name=%s, author=%s, publisher=%s, isbn=%s, price=%s, total=%s, stock=%s, created_at=%s WHERE id=%s"
self.cursor.execute(sql, (book.name, book.author, book.publisher, book.isbn, book.price, book.total, book.stock, book.created_at, book.id))
self.conn.commit()
def delete_book(self, id):
sql = "DELETE FROM book WHERE id=%s"
self.cursor.execute(sql, id)
self.conn.commit()
def get_books(self):
sql = "SELECT * FROM book"
self.cursor.execute(sql)
rows = self.cursor.fetchall()
books = []
for row in rows:
book = Book(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8])
books.append(book)
return books
def insert_reader(self, reader):
sql = "INSERT INTO reader (name, gender, tel, created_at) VALUES (%s, %s, %s, %s)"
self.cursor.execute(sql, (reader.name, reader.gender, reader.tel, reader.created_at))
self.conn.commit()
def update_reader(self, reader):
sql = "UPDATE reader SET name=%s, gender=%s, tel=%s, created_at=%s WHERE id=%s"
self.cursor.execute(sql, (reader.name, reader.gender, reader.tel, reader.created_at, reader.id))
self.conn.commit()
def delete_reader(self, id):
sql = "DELETE FROM reader WHERE id=%s"
self.cursor.execute(sql, id)
self.conn.commit()
def get_readers(self):
sql = "SELECT * FROM reader"
self.cursor.execute(sql)
rows = self.cursor.fetchall()
readers = []
for row in rows:
reader = Reader(row[0], row[1], row[2], row[3], row[4])
readers.append(reader)
return readers
def insert_borrow(self, borrow):
sql = "INSERT INTO borrow (book_id, reader_id, borrow_date, return_date, created_at) VALUES (%s, %s, %s, %s, %s)"
self.cursor.execute(sql, (borrow.book_id, borrow.reader_id, borrow.borrow_date, borrow.return_date, borrow.created_at))
self.conn.commit()
def update_borrow(self, borrow):
sql = "UPDATE borrow SET book_id=%s, reader_id=%s, borrow_date=%s, return_date=%s, created_at=%s WHERE id=%s"
self.cursor.execute(sql, (borrow.book_id, borrow.reader_id, borrow.borrow_date, borrow.return_date, borrow.created_at, borrow.id))
self.conn.commit()
def delete_borrow(self, id):
sql = "DELETE FROM borrow WHERE id=%s"
self.cursor.execute(sql, id)
self.conn.commit()
def get_borrows(self):
sql = "SELECT * FROM borrow"
self.cursor.execute(sql)
rows = self.cursor.fetchall()
borrows = []
for row in rows:
borrow = Borrow(row[0], row[1], row[2], row[3], row[4], row[5])
borrows.append(borrow)
return borrows
def insert_fine(self, fine):
sql = "INSERT INTO fine (borrow_id, amount, created_at) VALUES (%s, %s, %s)"
self.cursor.execute(sql, (fine.borrow_id, fine.amount, fine.created_at))
self.conn.commit()
def update_fine(self, fine):
sql = "UPDATE fine SET borrow_id=%s, amount=%s, created_at=%s WHERE id=%s"
self.cursor.execute(sql, (fine.borrow_id, fine.amount, fine.created_at, fine.id))
self.conn.commit()
def delete_fine(self, id):
sql = "DELETE FROM fine WHERE id=%s"
self.cursor.execute(sql, id)
self.conn.commit()
def get_fines(self):
sql = "SELECT * FROM fine"
self.cursor.execute(sql)
rows = self.cursor.fetchall()
fines = []
for row in rows:
fine = Fine(row[0], row[1], row[2], row[3])
fines.append(fine)
return fines
def close(self):
self.cursor.close()
self.conn.close()
```
在Python代码中,先定义了Book、Reader、Borrow、Fine类,用于封装数据库中的数据。然后定义了Library类,用于连接数据库,并实现对数据库的增删改查操作。
3. 编写图形界面
最后,使用PyQt等GUI框架编写图形界面,将用户操作与Python代码结合起来,实现完整的图书信息管理系统。具体编写过程可以参考相关的GUI框架文档和教程。
阅读全文