Python实现图书借阅管理系统
1. 系统概述
本系统是基于Python语言实现的,用于管理图书借阅流程的软件。系统包括以下三个模块:
- 图书管理模块:用于管理图书信息,包括书名、作者、出版社等
- 读者管理模块:用于管理读者信息,包括姓名、性别、年龄等
- 借阅管理模块:用于管理借阅流程,包括借出图书、归还图书等
2. 实现过程
2.1 数据库设计
在开发系统前,我们需要设计数据库存储图书和读者信息,使用SQLite数据库进行存储。下面是数据库的结构设计:
-- 图书表
CREATE TABLE book (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
press TEXT NOT NULL,
total INTEGER NOT NULL,
stock INTEGER NOT NULL
);
-- 读者表
CREATE TABLE reader (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
gender TEXT NOT NULL,
age INTEGER NOT NULL,
phone TEXT NOT NULL
);
-- 借阅表
CREATE TABLE borrow (
id INTEGER PRIMARY KEY AUTOINCREMENT,
book_id INTEGER NOT NULL,
reader_id INTEGER NOT NULL,
borrow_date TEXT NOT NULL,
return_date TEXT NOT NULL,
FOREIGN KEY(book_id) REFERENCES book(id),
FOREIGN KEY(reader_id) REFERENCES reader(id)
);
2.2 实现模块
2.2.1 图书管理模块
图书管理模块包括添加图书、删除图书、修改图书和查询图书信息等功能。
添加图书的实现:
def add_book(title, author, press, total):
stock = total
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('INSERT INTO book(title, author, press, total, stock) \
VALUES (?, ?, ?, ?, ?)', (title, author, press, total, stock))
conn.commit()
conn.close()
print('Add book successfully!')
删除图书的实现:
def delete_book(book_id):
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('DELETE FROM book WHERE id=?', (book_id,))
conn.commit()
conn.close()
print('Delete book successfully!')
2.2.2 读者管理模块
读者管理模块包括添加读者、删除读者、修改读者和查询读者信息等功能。
添加读者的实现:
def add_reader(name, gender, age, phone):
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('INSERT INTO reader(name, gender, age, phone) VALUES (?, ?, ?, ?)', (name, gender, age, phone))
conn.commit()
conn.close()
print('Add reader successfully!')
修改读者的实现:
def update_reader(reader_id, name, gender, age, phone):
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('UPDATE reader SET name=?, gender=?, age=?, phone=? WHERE id=?', (name, gender, age, phone, reader_id))
conn.commit()
conn.close()
print('Update reader successfully!')
2.2.3 借阅管理模块
借阅管理模块包括借出图书、归还图书和查询借阅记录等功能。
借出图书的实现:
def borrow_book(book_id, reader_id, borrow_date):
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('SELECT stock FROM book WHERE id=?', (book_id,))
stock = cur.fetchone()[0]
if stock == 0:
print('The book is out of stock!')
else:
cur.execute('UPDATE book SET stock=? WHERE id=?', (stock - 1, book_id))
cur.execute('INSERT INTO borrow(book_id, reader_id, borrow_date, return_date) VALUES (?, ?, ?, ?)',
(book_id, reader_id, borrow_date, ''))
conn.commit()
print('Borrow book successfully!')
conn.close()
归还图书的实现:
def return_book(borrow_id, return_date):
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('SELECT book_id FROM borrow WHERE id=?', (borrow_id,))
book_id = cur.fetchone()[0]
cur.execute('SELECT stock FROM book WHERE id=?', (book_id,))
stock = cur.fetchone()[0]
cur.execute('UPDATE book SET stock=? WHERE id=?', (stock + 1, book_id))
cur.execute('UPDATE borrow SET return_date=? WHERE id=?', (return_date, borrow_id))
conn.commit()
conn.close()
print('Return book successfully!')
2.3 示例说明
2.3.1 添加图书和查询图书
# 添加图书
add_book('Python从入门到精通', '张三', '清华大学出版社', 100)
# 查询图书
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('SELECT * FROM book')
row = cur.fetchall()
for book in row:
print(f'{book[1]}, {book[2]}, {book[3]}, 库存:{book[5]}本')
conn.close()
2.3.2 借出图书和归还图书
# 借出图书
borrow_book(1, 1, '2021-03-01')
# 查询借阅记录
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('SELECT borrow.id, book.title, reader.name, borrow.borrow_date, borrow.return_date \
FROM borrow INNER JOIN book ON borrow.book_id=book.id \
INNER JOIN reader ON borrow.reader_id=reader.id')
row = cur.fetchall()
for borrow in row:
print(f'{borrow[0]}, {borrow[1]}, {borrow[2]}, 借出日期:{borrow[3]}')
conn.close()
# 归还图书
return_book(1, '2021-03-05')
# 查询借阅记录
conn = sqlite3.connect('library.db')
cur = conn.cursor()
cur.execute('SELECT borrow.id, book.title, reader.name, borrow.borrow_date, borrow.return_date \
FROM borrow INNER JOIN book ON borrow.book_id=book.id \
INNER JOIN reader ON borrow.reader_id=reader.id')
row = cur.fetchall()
for borrow in row:
print(f'{borrow[0]}, {borrow[1]}, {borrow[2]}, 借出日期:{borrow[3]}, 归还日期:{borrow[4]}')
conn.close()
3. 总结
本系统实现了基本的图书借阅管理功能,涉及到的知识点包括SQLite数据库操作和Python函数实现。在实现过程中,我们需要仔细设计数据库结构,同时编写代码时也需要注意异常处理。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python实现图书借阅管理系统 - Python技术站