好的,为了实现这个图书借阅管理系统,我们可以考虑以下几个步骤:
1. 设计数据库结构,包括书籍、用户和借阅记录等表的字段和关系。
2. 使用 Python 编写程序,创建数据库并初始化表结构。
3. 编写程序实现管理员和用户登录功能,管理员可以添加、删除和修改书籍信息,用户可以查询、借阅和归还书籍。
4. 实现图书查询功能,可以按照书名、作者、出版社等条件进行查询。
5. 实现借阅和归还功能,包括借阅时间和归还时间的记录。
6. 实现图书的统计功能,包括借阅情况、借阅排行榜等。
下面是一个简单的示例代码,代码中使用了 SQLite 数据库和 Flask 框架来实现:
```python
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
# 连接数据库
conn = sqlite3.connect('library.db')
c = conn.cursor()
# 创建书籍、用户和借阅记录表
c.execute('''CREATE TABLE IF NOT EXISTS books
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
publisher TEXT NOT NULL,
isbn TEXT NOT NULL,
quantity INTEGER NOT NULL)''')
c.execute('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
password TEXT NOT NULL,
is_admin INTEGER NOT NULL)''')
c.execute('''CREATE TABLE IF NOT EXISTS borrow_records
(id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
borrow_time TEXT NOT NULL,
return_time TEXT)''')
# 插入初始数据
c.execute("INSERT INTO users (name, password, is_admin) VALUES ('admin', 'admin', 1)")
c.execute("INSERT INTO books (title, author, publisher, isbn, quantity) VALUES ('Python编程从入门到实践', 'Eric Matthes', '人民邮电出版社', '9787115428021', 10)")
c.execute("INSERT INTO books (title, author, publisher, isbn, quantity) VALUES ('流畅的Python', 'Luciano Ramalho', '人民邮电出版社', '9787111601107', 5)")
conn.commit()
# 关闭数据库连接
conn.close()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
name = request.form['name']
password = request.form['password']
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("SELECT * FROM users WHERE name=? AND password=?", (name, password))
user = c.fetchone()
conn.close()
if user is None:
return render_template('login.html', error='用户名或密码不正确')
else:
if user[3] == 1:
return redirect(url_for('admin'))
else:
return redirect(url_for('user'))
else:
return render_template('login.html')
@app.route('/admin')
def admin():
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("SELECT * FROM books")
books = c.fetchall()
conn.close()
return render_template('admin.html', books=books)
@app.route('/add_book', methods=['POST'])
def add_book():
title = request.form['title']
author = request.form['author']
publisher = request.form['publisher']
isbn = request.form['isbn']
quantity = request.form['quantity']
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("INSERT INTO books (title, author, publisher, isbn, quantity) VALUES (?, ?, ?, ?, ?)", (title, author, publisher, isbn, quantity))
conn.commit()
conn.close()
return redirect(url_for('admin'))
@app.route('/delete_book/<int:book_id>')
def delete_book(book_id):
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("DELETE FROM books WHERE id=?", (book_id,))
conn.commit()
conn.close()
return redirect(url_for('admin'))
@app.route('/user')
def user():
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("SELECT * FROM books")
books = c.fetchall()
c.execute("SELECT * FROM borrow_records WHERE user_id=?", (1,))
records = c.fetchall()
conn.close()
return render_template('user.html', books=books, records=records)
@app.route('/borrow_book/<int:book_id>')
def borrow_book(book_id):
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("SELECT * FROM books WHERE id=?", (book_id,))
book = c.fetchone()
if book[5] > 0:
c.execute("UPDATE books SET quantity=? WHERE id=?", (book[5]-1, book_id))
c.execute("INSERT INTO borrow_records (user_id, book_id, borrow_time) VALUES (?, ?, datetime('now', 'localtime'))", (1, book_id))
conn.commit()
conn.close()
return redirect(url_for('user'))
@app.route('/return_book/<int:record_id>')
def return_book(record_id):
conn = sqlite3.connect('library.db')
c = conn.cursor()
c.execute("SELECT * FROM borrow_records WHERE id=?", (record_id,))
record = c.fetchone()
c.execute("SELECT * FROM books WHERE id=?", (record[2],))
book = c.fetchone()
c.execute("UPDATE books SET quantity=? WHERE id=?", (book[5]+1, book[0]))
c.execute("UPDATE borrow_records SET return_time=datetime('now', 'localtime') WHERE id=?", (record_id,))
conn.commit()
conn.close()
return redirect(url_for('user'))
if __name__ == '__main__':
app.run(debug=True)
```
其中,`index.html`、`login.html`、`admin.html` 和 `user.html` 分别是网页模板文件,可以根据需要进行修改。运行该程序后,访问 `http://localhost:5000/` 即可使用该图书借阅管理系统。