当前位置:   article > 正文

基于python+MySQL编写图书管理系统_图书管理系统数据库代码

图书管理系统数据库代码

目录

一、功能

二、表

三、代码编写


一、功能

二、表

books表:

 reader表:

 

三、代码编写

  1. import pymysql
  2. import datetime
  3. # 建立数据库连接
  4. connection = pymysql.connect(
  5. host='localhost', # 数据库主机名
  6. port=3306, # 数据库端口号,默认为3306
  7. user='root', # 数据库用户名
  8. passwd='123456', # 数据库密码
  9. db='tushuguanlixitong', # 数据库名称
  10. charset='utf8' # 字符编码
  11. )
  12. def query(sql, one=False):
  13. cursor = connection.cursor()
  14. cursor.execute(sql)
  15. if one:
  16. return cursor.fetchone()
  17. else:
  18. return cursor.fetchall()
  19. def update(sql):
  20. cursor = connection.cursor()
  21. result = cursor.execute(sql)
  22. # 提交事务
  23. connection.commit()
  24. return result
  25. def select_book():
  26. # 创建一个数据库游标对象
  27. cursor = connection.cursor()
  28. # 定义SQL查询语句,从图书信息表表中选择所有数据
  29. sql = 'select*from books;'
  30. # 执行SQL查询语句
  31. cursor.execute(sql)
  32. # 提交事务,确保数据被正确写入数据库
  33. connection.commit()
  34. # 从游标中获取查询结果,保存到data变量中
  35. data = cursor.fetchall()
  36. # 导入pandas库中的DataFrame类
  37. from pandas import DataFrame
  38. # 显示DataFrame的前5行数据
  39. df = DataFrame(data, columns=['book_name', 'book_id', 'book_status', 'book_ISBN', 'author', 'press', 'borrower',
  40. 'loan_time'])
  41. df.head()
  42. print(df.head())
  43. class Book:
  44. def __init__(self, book_name, book_id, book_status, book_isbn, author, press):
  45. self.book_name = book_name
  46. self.author = author
  47. self.book_id = book_id
  48. self.book_status = book_status
  49. self.book_ISBN = book_isbn
  50. self.press = press
  51. def add_book():
  52. cursor = connection.cursor()
  53. sql = ('INSERT INTO books (book_name,book_id,book_status,book_ISBN,author,press)'
  54. 'VALUES(%s,%s,%s,%s,%s,%s)')
  55. print('请输入添加图书信息:')
  56. book = Book(None, None, None, None, None, None)
  57. book.book_name = input('请输入图书名:')
  58. book.book_id = input('请输入图书编号:')
  59. book.book_status = input('请输入图书状态:')
  60. book.book_ISBN = input('请输入图书ISBN码:')
  61. book.author = input('请输入图书作者:')
  62. book.press = input('请输入图书出版社:')
  63. values = (book.book_name, book.book_id, book.book_status, book.book_ISBN, book.author, book.press)
  64. cursor.execute(sql, values)
  65. connection.commit()
  66. print('图书添加成功')
  67. def delete_book():
  68. cursor = connection.cursor()
  69. book_id = input("输入需要删除的图书号:")
  70. result = query("select * FROM books where book_id = {}".format(book_id), one=True)
  71. if result:
  72. print("图书信息:".format(result))
  73. chooice = input("是否删除? 1.yes,2.no")
  74. if chooice == '1':
  75. update("DELETE FROM books where book_id = {}".format(book_id))
  76. print("成功删除")
  77. else:
  78. print("放弃删除")
  79. else:
  80. print("未查询到相关书籍信息~")
  81. num = input("继续删除请输入1, 回车退回主菜单")
  82. if num == "1":
  83. delete_book()
  84. def update_book():
  85. cursor = connection.cursor()
  86. book_id = input("输入需要删除的图书号:")
  87. result = query("select * FROM books where book_id = {}".format(book_id), one=True)
  88. if result:
  89. print("图书信息:".format(result))
  90. book_name = input("请输入修改书名:")
  91. book_status = input("请输入图书状态:")
  92. author = input("请输入修改作者:")
  93. press = input("请输入修改出版社:")
  94. update("update books set book_name = '{}',book_status = '{}',author = '{}',press = '{}' where book_id = {};"
  95. .format(book_name, book_status, author, press, book_id))
  96. print("更新成功")
  97. else:
  98. print("未查询到相关书籍信息~")
  99. num = input("继续更新请输入1, 回车退回主菜单")
  100. if num == "1":
  101. update_book()
  102. def select_reader():
  103. # 创建一个数据库游标对象
  104. cursor = connection.cursor()
  105. # 定义SQL查询语句,从图书信息表表中选择所有数据
  106. sql = 'select*from reader;'
  107. # 执行SQL查询语句
  108. cursor.execute(sql)
  109. # 提交事务,确保数据被正确写入数据库
  110. connection.commit()
  111. # 从游标中获取查询结果,保存到data变量中
  112. data = cursor.fetchall()
  113. # 导入pandas库中的DataFrame类
  114. from pandas import DataFrame
  115. # 显示DataFrame的前5行数据
  116. df = DataFrame(data, columns=['姓名', '编号', '身份', '部门'])
  117. df.head()
  118. print(df.head())
  119. # 查询读者名单
  120. class Read:
  121. def __init__(self, read_name, read_id, read_identity, read_department):
  122. self.read_name = read_name
  123. self.read_id = read_id
  124. self.read_identity = read_identity
  125. self.read_department = read_department
  126. def add_reader():
  127. cursor = connection.cursor()
  128. sql = 'INSERT INTO reader (read_name,read_id,read_identity,read_department) VALUES(%s,%s,%s,%s)'
  129. print('添加人员:')
  130. read = Read(None, None, None, None)
  131. read.read_name = input('请输入读者姓名')
  132. read.read_id = input('请输入读者编号')
  133. read.read_identity = input('请输入读者身份')
  134. read.read_department = input('请输入读者所在部门')
  135. values = (read.read_name, read.read_id, read.read_identity, read.read_department)
  136. cursor.execute(sql, values)
  137. connection.commit()
  138. print('读者添加成功!')
  139. def borrow_book():
  140. cursor = connection.cursor()
  141. book_id = input("请输入需要借阅的图书号:")
  142. result = query("select * from books where book_id={};".format(book_id), one=True)
  143. print(result)
  144. if result:
  145. if result[2] == "出借":
  146. print("抱歉,该书已经借出!")
  147. else:
  148. while True:
  149. borrower = input("请输入借阅者的名字:")
  150. if result:
  151. return_time = input("请输入还书的时间;")
  152. update("update books set return_time='{}' where book_id={};".format(return_time, book_id))
  153. if borrower:
  154. update("update books set borrower='{}' where book_id={};".format(borrower, book_id))
  155. update("update books set book_status='出借' where book_id={};".format(book_id))
  156. print("图书借阅成功~")
  157. break
  158. else:
  159. print("没有这个读者,请重新输入")
  160. else:
  161. print("未查询到相关书籍信息~")
  162. num = input("继续借阅请输入1, 回车退回主菜单")
  163. if num == "1":
  164. borrow_book()
  165. def back_book():
  166. cursor = connection.cursor()
  167. book_id = input('请输入要归还的图书书号:')
  168. result = query("select * from books where book_id={};".format(book_id), one=True)
  169. if result:
  170. if result[2] == '在架':
  171. print("抱歉,该书在架请确认编号是否正确!")
  172. else:
  173. update("update books set borrower='' where book_id={};".format(book_id))
  174. update("update books set book_status='在架' where book_id={};".format(book_id))
  175. print("归还成功~")
  176. else:
  177. print("未查询到相关书籍信息~")
  178. num = input("继续还书请输入1, 回车退回主菜单")
  179. if num == "1":
  180. back_book()
  181. def time():
  182. cursor = connection.cursor()
  183. now = datetime.datetime.now()
  184. cursor .execute("SELECT * FROM books WHERE return_time < %s", (now,))
  185. book_name = cursor.fetchall()
  186. print("图书归还超期:")
  187. print(book_name)
  188. # 查询临期图书(距离当前时间一周内到期)
  189. cursor.execute("SELECT * FROM books WHERE return_time BETWEEN %s AND %s",
  190. (now, now + datetime.timedelta(days=10)))
  191. due_soon_books = cursor.fetchall()
  192. print("距离图书(到期还有10天):")
  193. def menu(): # 图书管理系统菜单
  194. while True:
  195. print("""
  196. 图书管理系统
  197. 1.查询图书
  198. 2.增加图书
  199. 3.借阅图书
  200. 4.归还图书
  201. 5.修改图书
  202. 6.删除图书
  203. 7.导入读者名单
  204. 8.查看读者名单
  205. 9.超期和临期查询
  206. 10.退出系统
  207. """)
  208. choice = input('请选择:')
  209. if choice == '1':
  210. select_book()
  211. elif choice == '2':
  212. add_book()
  213. elif choice == '3':
  214. borrow_book()
  215. elif choice == '4':
  216. back_book()
  217. elif choice == '5':
  218. update_book()
  219. elif choice == '6':
  220. delete_book()
  221. elif choice == '7':
  222. add_reader()
  223. elif choice == '8':
  224. select_reader()
  225. elif choice == '9':
  226. time()
  227. elif choice == '10':
  228. print('欢迎下次使用~~~~~~~')
  229. break
  230. else:
  231. print('请输入正确序号')
  232. menu()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/747766
推荐阅读
相关标签
  

闽ICP备14008679号