本课题要求编写Python程序实现对图书信息录入、图书信息查询、图书借阅等方面的管理。一个综合的图书借阅管理系统,要求能够管理图书的基本信息(包含新图书入库、读者图书查询借阅等),需要实现以下功能:读取以数据文件形式存储的图书信息;管理员可以增加、修改、删除图书的信息;读者可以按照图书名、作者名、索书号等查询图书,并可通过该系统实现对图书的借阅、续借和归还;读者还可查询自己所借图书的信息(是否归还、归还日期等)。
要能提供以下几个基本功能:
(1)系统内的相关信息文件由程序设计人员预先从键盘上录入,文件中的数据记录不得少于20条;
(2)设计并实现系统的相关界面,提供良好的交互界面;
(3)登录时输入帐号以区分读者和管理员;
(4)读者信息查询:
- 图书查询借阅功能:输入一个书名(或索书号、作者等其他信息),查出相关图书的基本信息并显示输出,同时提示是否需要借阅该图书;
- “我的”功能:显示个人图书借阅历史,显示所借图书的状态(是否归还、归还日期)、并选择是否归还或续借。
(5)管理端信息查询:
管理员可以增加、修改、删除图书的信息。
一 、导入需要的模块
import pymsql
二、完成user登录等操作
import pymysql
from hashlib import md5
database = pymysql.connect(
host='127.0.0.1',
user='root',
password='xxxxx',
charset='utf8',
port=3306,
database='spider10'
)
cursor = database.cursor(pymysql.cursors.DictCursor)
def register_user():
salt = '我是盐'.encode('utf-8')
obj = md5(salt)
id = input('请输入用户名:')
password = input('请输入密码:')
obj.update(password.encode('utf-8'))
digest_password = obj.hexdigest()
# 判断注册的用户名是否已经存在,如果存在,则提示用户重新输入用户名
sql1 = """
select id from spider10.login_user2 where id=%s
"""
cursor.execute(sql1, (id,))
ret = cursor.fetchone()
if ret:
print('该用户名已存在!请重新输入用户名!')
register_user()
else:
sql = """
insert into spider10.login_user2(id,pwd) values(%s,%s)
"""
cursor.execute(sql, (id, digest_password))
database.commit()
print('账号注册成功!是否需要登录?(y/n):')
choice = input()
if choice == 'y':
login_user()
else:
print('谢谢使用!,再见!')
return True
# id=user
# pwd=hyh
def login_user():
count = 0
salt = '我是盐'.encode('utf-8')
obj = md5(salt)
id = input('请输入用户名:')
password = input('请输入密码:')
obj.update(password.encode('utf-8'))
digest_password = obj.hexdigest()
sql = """
select * from spider10.login_user2 where id=%s
"""
cursor.execute(sql, (id,))
result = cursor.fetchone()
while True:
if result is None:
print('未找到该用户!请选择注册或者重置密码!')
choice = input('请选择(1.注册新用户 2.重置密码):')
if choice == '1':
register_user()
elif choice == '2':
reset_password()
break
else:
if result.get('id') == id and result.get('pwd') == digest_password:
print('登录成功!')
print('欢迎进入图书管理系统!')
break
else:
print('登录失败!')
count += 1
if count == 3:
print('密码错误次数过多,请尝试重置密码!')
reset_password()
break
# 如果用户输入3次密码错误,则提示可以尝试重置密码
# 在知道用户名后,可以重置密码
def reset_password():
salt = '我是盐'.encode('utf-8')
obj = md5(salt)
id = input('请输入原始用户名:')
password = input('请输入重置后的密码:')
obj.update(password.encode('utf-8'))
digest_password = obj.hexdigest()
# 判断输入的用户名是否存在,如果存在,可以修改密码,如果不存在,提示用户重新注册
sql1 = """
select * from spider10.login_user2 where id=%s
"""
cursor.execute(sql1, (id,))
res = cursor.fetchone()
if res is None:
print('未找到该用户!请选择(1.注册新用户注册 2.重试!3.退出!):')
choice = input()
if choice == '1':
register_user()
elif choice == '2':
reset_password()
elif choice == '3':
print('谢谢使用!,再见!')
exit()
sql = """
select * from spider10.login_user2 where id=%s
"""
cursor.execute(sql, (id,))
result = cursor.fetchall()
if result:
for i in result:
if i['id'] == id:
sql = """
update spider10.login_user2 set pwd=%s where id=%s
"""
cursor.execute(sql, (digest_password, id))
database.commit()
print('密码重置成功!')
print('是否需要登录?(y/n):')
choice = input()
if choice == 'y':
login_user()
else:
print('谢谢使用!,再见!')
exit()
break
else:
print('密码重置失败!')
break
else:
print('未找到该用户!请先注册或者重试!')
choice = input('请选择(1.注册新用户 2.重试):')
if choice == '1':
register_user()
elif choice == '2':
reset_password()
# database.close()
# 登录成功后,可以进行图书管理系统的操作
if __name__ == '__main__':
login_user()
数据库密码写自己的。
三 、介绍代码(较为简单)
这里使用MD5摘要算法,不可以解密,为了防止被撞库破解,所以采用加盐处理可以有效防止被破解。salt=“我是盐”。这次跟上一个版本不同在于,这个链接了数据库mysql,之前只是对于文件操作。