当前位置:   article > 正文

数据库课程设计python——图书馆管理系统_图书馆数据课设计

图书馆数据课设计

对我来说挺大一个工作内容了,用户操作界面的代码是从别人的文章里拿来然后改的,文章有时间我找找会放进来

功能与结构

构建数据库(本地)

用的是SQL server,根据逻辑结构来建表,以及触发器

建表

  1. CREATE DATABASE LibraryManage ON
  2. PRIMARY
  3. (NAME = xsgl_Data, FILENAME = 'D:\DBF\LibraryManage.mdf', FILEGROWTH = 5)
  4. LOG ON
  5. (NAME = xsgl_Log, FILENAME = 'D:\DBF\LibraryManage.ldf', FILEGROWTH = 5)
  6. GO
  7. USE LibraryManage
  8. CREATE TABLE Users
  9. (u_id CHAR(10) PRIMARY KEY,
  10. u_password VARCHAR(20) NOT NULL,
  11. u_Admin BIT DEFAULT 0,
  12. u_name VARCHAR(20) NOT NULL,
  13. user_phone CHAR(11),
  14. user_adress VARCHAR(60),
  15. )
  16. CREATE TABLE Books
  17. (book_id CHAR(10) PRIMARY KEY,
  18. book_name VARCHAR(40),
  19. author VARCHAR(40),
  20. press VARCHAR(40),
  21. collocation_num INT,
  22. lendable INT,
  23. CONSTRAINT check_lendable CHECK (lendable <= collocation_num)
  24. )
  25. CREATE TABLE Records
  26. (Record_id INT IDENTITY(100000,1) PRIMARY KEY,
  27. u_id CHAR(10) FOREIGN KEY (u_id) REFERENCES Users(u_id),
  28. book_id CHAR(10) FOREIGN KEY (book_id) REFERENCES Books(book_id),
  29. lend_time DATETIME,
  30. return_time DATETIME
  31. )
  32. CREATE TABLE Lending
  33. (Record_id INT,
  34. u_id CHAR(10),
  35. book_id CHAR(10),
  36. lend_time DATETIME
  37. FOREIGN KEY (Record_id) REFERENCES Records(Record_id)
  38. )
  39. INSERT INTO Users(u_id, u_password, u_Admin, u_name, user_phone)
  40. VALUES('0000000100','666', 1, '巩啊', '13912345678')
  41. INSERT INTO Users(u_id, u_password, u_name, user_phone)
  42. VALUES
  43. ('0000000101','yqowzrxcwe', '李梓萱', '13812345678'),
  44. ('0000000102','mnahsduf', '张婧宸', '13712345678'),
  45. ('0000000103','cxnjmqzp', '赵奕涵', '13612345678'),
  46. ('0000000104','skwopdtnbf', '刘子怡', '13512345678'),
  47. ('0000000105','qweasdzxc', '陈思婷', '13412345678'),
  48. ('0000000106','poiuytrewq', '杨晨阳', '13312345678'),
  49. ('0000000107','ljhgfdsazx', '吴思涵', '18212345678'),
  50. ('0000000108','ytbvfrde', '周俊逸', '18112345678'),
  51. ('0000000109','mnbvcxzlkj', '孙雅菲', '18012345678'),
  52. ('0000000110','qwertyuiop', '郭雨莹', '15912345678'),
  53. ('0000000111','zxcvbnmasd', '唐子涵', '15812345678'),
  54. ('0000000112','asdfghjkl', '罗雨柔', '15712345678'),
  55. ('0000000113','poiuytrewq', '贾子萱', '15612345678'),
  56. ('0000000114','lkjhgfdsa', '尹忆萱', '15512345678'),
  57. ('0000000115','zxcvbnm', '段晨雨', '15412345678'),
  58. ('0000000116','asdfgh', '黄梓涵', '15312345678'),
  59. ('0000000117','qwerty', '董思涵', '15212345678'),
  60. ('0000000118','zxcvbn', '梁婧怡', '15112345678'),
  61. ('0000000119','asdfghj', '叶雨萱', '15012345678'),
  62. ('0000000120','poiuytre', '钟子涵', '14912345678'),
  63. ('0000000121','lkjhgf', '姚雅菲', '14812345678'),
  64. ('0000000122','zxcvbnm', '贺晨雨', '14712345678'),
  65. ('0000000123','qazwsx', '方梓涵', '14612345678'),
  66. ('0000000124','rfvtgb', '石晨阳', '14512345678'),
  67. ('0000000125','edcrfv', '常子萱', '14412345678'),
  68. ('0000000126','tgbnhy', '韩忆萱', '14312345678'),
  69. ('0000000127','ujmik,', '龙晨雨', '14212345678'),
  70. ('0000000128','yhnmju', '毛梓涵', '14112345678'),
  71. ('0000000129','poiuhg', '万雨萱', '14012345678'),
  72. ('0000000130','rewqas', '宋子涵', '13912345678')
  73. INSERT INTO Books (book_id, book_name, author, press, collocation_num, lendable)
  74. VALUES
  75. ('1000000000', '骆驼祥子', '老舍', '北京教育出版社', 20, 18),
  76. ('1000000001', '围城', '钱钟书', '人民文学出版社', 30, 28),
  77. ('1000000002', '活着', '余华', '作家出版社', 25, 23),
  78. ('1000000003', '1984', '乔治·奥威尔', '南海出版公司', 15, 12),
  79. ('1000000004', '平凡的世界', '路遥', '人民文学出版社', 40, 37),
  80. ('1000000005', '白夜行', '东野圭吾', '南海出版公司', 18, 15),
  81. ('1000000006', '红楼梦', '曹雪芹', '人民文学出版社', 35, 32),
  82. ('1000000007', '傲慢与偏见', '简·奥斯汀', '外语教学与研究出版社', 22, 19),
  83. ('1000000008', '三体', '刘慈欣', '重庆出版社', 28, 25),
  84. ('1000000009', '追风筝的人', '卡勒德·胡赛尼', '上海人民出版社', 33, 30),
  85. ('1000000010', '小王子', '圣埃克苏佩里', '人民文学出版社', 20, 17),
  86. ('1000000012', '嫌疑人X的献身', '东野圭吾', '南海出版公司', 30, 27),
  87. ('1000000013', '百年孤独', '加西亚·马尔克斯', '南海出版公司', 38, 34),
  88. ('1000000014', '人类简史', '尤瓦尔·赫拉利', '中信出版社', 26, 24),
  89. ('1000000015', '解忧杂货店', '东野圭吾', '南海出版公司', 23, 20),
  90. ('1000000017', '月亮与六便士', '毛姆', '人民文学出版社', 29, 27),
  91. ('1000000018', '白鹿原', '陈忠实', '作家出版社', 27, 24),
  92. ('1000000019', '战争与和平', '列夫·托尔斯泰', '人民文学出版社', 32, 29),
  93. ('1000000020', '偷影子的人', '马克·李维', '北京联合出版公司', 19, 17),
  94. ('1000000021', '囚鸟', '冯内古特', '上海译文出版社', 24, 21),
  95. ('1000000022', '乌合之众', '古斯塔夫·勒庞', '中华书局', 28, 25),
  96. ('1000000023', '基督山伯爵', '大仲马', '上海译文出版社', 32, 29),
  97. ('1000000025', '金字塔原理', '芭芭拉·明托', '机械工业出版社', 20, 17),
  98. ('1000000026', '肖申克的救赎', '斯蒂芬·金', '人民文学出版社', 30, 27),
  99. ('1000000027', '福尔摩斯探案集', '阿瑟·柯南·道尔', '北京联合出版公司', 22, 19),
  100. ('1000000028', '非暴力沟通', '马维·罗森伯格', '中信出版社', 18, 15),
  101. ('1000000030', '霍乱时期的爱情', '加西亚·马尔克斯', '南海出版公司', 34, 30),
  102. ('1000000031', '时间简史', '斯蒂芬·霍金', '湖南科学技术出版社', 20, 17),
  103. ('1000000032', '人间失格', '太宰治', '筑摩书房', 25, 22),
  104. ('1000000033', '飘', '玛格丽特·米切尔', '上海人民出版社', 28, 25),
  105. ('1000000034', '月亮与六便士', '毛姆', '人民文学出版社', 33, 30),
  106. ('1000000035', '杀死一只知更鸟', '哈珀·李', '译林出版社', 24, 21),
  107. ('1000000036', '黄金时代', '王小波', '作家出版社', 27, 24),
  108. ('1000000038', '盗墓笔记', '南派三叔', '民主与建设出版社', 30, 27)

触发器

借书还书操作通过触发器来设计的,借阅记录表是Records、储存当前借出的书籍的表是Lending

当用户借书时,

借阅记录中增加一条记录
并且将记录也添加到当前借阅的书
更新书籍信息,可借数量减少一本

当用户还书时,

更新借阅记录之中的还书时间
从当前借阅的书中删除相应记录,
更新书籍信息,可借数量增加一本

  1. GO
  2. CREATE TRIGGER when_lend ON Records
  3. INSTEAD OF INSERT
  4. AS
  5. DECLARE @insertUID char(10),@insertBID char(10),@rest INT,
  6. @time datetime
  7. SELECT @insertUID=u_id,@insertBID=book_id
  8. FROM inserted
  9. SELECT @rest=lendable
  10. FROM Books
  11. WHERE book_id=@insertBID
  12. IF @rest<=0
  13. BEGIN
  14. PRINT('该书不足')
  15. END
  16. ELSE
  17. BEGIN
  18. SET @time=getdate()
  19. INSERT INTO Records(u_id,book_id,lend_time)
  20. SELECT u_id,book_id,@time
  21. FROM inserted
  22. DECLARE @RID INT
  23. SET @RID = SCOPE_IDENTITY()
  24. INSERT INTO Lending
  25. VALUES (@RID,@insertUID,@insertBID,@time)
  26. UPDATE Books
  27. SET lendable=lendable-1
  28. WHERE book_id=@insertBID
  29. END
  30. GO
  31. CREATE TRIGGER when_return ON Lending
  32. AFTER DELETE
  33. AS
  34. DECLARE @UID char(10),@BID char(10),@RID INT,
  35. @time datetime
  36. SELECT @RID=Record_id,@UID=u_id,@BID=book_id
  37. FROM deleted
  38. BEGIN
  39. SET @time=getdate()
  40. UPDATE Records
  41. SET return_time=@time
  42. WHERE Record_id=@RID
  43. UPDATE Books
  44. SET lendable=lendable+1
  45. WHERE book_id=@BID
  46. END

操作与用户界面python

首页

  1. import pymssql
  2. from tkinter import ttk
  3. import tkinter as tk
  4. import tkinter.font as tkFont
  5. from tkinter import * # 图形界面库
  6. import tkinter.messagebox as messagebox # 弹窗
  7. class StartPage:
  8. def __init__(self, parent_window):
  9. parent_window.destroy() # 销毁子界面
  10. self.window = tk.Tk() # 初始框的声明
  11. self.window.title('图书借阅管理系统')
  12. self.window.geometry('600x400') # 这里的乘是小x
  13. label = Label(self.window, text="图书借阅管理系统", font=("Verdana", 20))
  14. label.pack(pady=80) # 界面的长度
  15. Button(self.window, text="用户登陆", font=tkFont.Font(size=16), command = lambda: UserLogin(self.window), width=30,\
  16. height=2,fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
  17. Button(self.window, text="注册", font=tkFont.Font(size=16), command = lambda: Register(), width=30, height=2,\
  18. fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
  19. Button(self.window, text='退出系统', height=2, font=tkFont.Font(size=16), width=30, command=self.window.destroy,\
  20. fg='white', bg='gray', activebackground='black', activeforeground='white').pack()
  21. self.window.mainloop() # 主消息循环

注册界面

  1. class Register:
  2. def __init__(self):
  3. self.window = tk.Tk()
  4. self.window.title('注册')
  5. self.window.geometry('300x450')
  6. label = tk.Label(self.window, text='用户注册', bg='green', font=('Verdana', 20), width=30, height=2)
  7. label.pack()
  8. Label(self.window, text='请输入账号(长度为10)', font=tkFont.Font(size=14)).pack(pady=10)
  9. self.u_id = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  10. self.u_id.pack()
  11. Label(self.window, text='请设置密码:', font=tkFont.Font(size=14)).pack(pady=10)
  12. self.u_password = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  13. self.u_password.pack()
  14. Label(self.window, text='请输入姓名:', font=tkFont.Font(size=14)).pack(pady=10)
  15. self.u_name = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  16. self.u_name.pack()
  17. Label(self.window, text='请输入电话:', font=tkFont.Font(size=14)).pack(pady=10)
  18. self.u_phone = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  19. self.u_phone.pack()
  20. Button(self.window, text="注册", width=8, font=tkFont.Font(size=12), command=self.register).pack(pady=40)
  21. self.window.mainloop() # 进入消息循环
  22. def register(self):
  23. db = pymssql.connect(server="localhost",database="LibraryManage", charset='utf8') # 打开数据库连接
  24. cursor = db.cursor() # 使用cursor()方法获取操作游标
  25. sql = "SELECT * FROM Users WHERE u_id = '%s'" % (self.u_id.get()) # SQL 查询语句
  26. cursor.execute(sql)
  27. results = cursor.fetchall()
  28. if len(self.u_id.get()) != 10:
  29. messagebox.showinfo('警告!', '请输入十位数字')
  30. return None
  31. if len(list(results)) != 0:
  32. messagebox.showinfo('警告!', '账号已被注册')
  33. return None
  34. try:
  35. sql = "INSERT INTO Users(u_id, u_password, u_name, user_phone) VALUES('%s','%s','%s','%s')" \
  36. %(self.u_id.get(), self.u_password.get(), self.u_name.get(), self.u_phone.get())
  37. cursor.execute(sql)
  38. db.commit()
  39. messagebox.showinfo('提示!', '注册成功!')
  40. self.window.destroy()
  41. except:
  42. messagebox.showinfo('警告!', '出错了')

登录界面

  1. #登陆页面
  2. class UserLogin:
  3. def __init__(self, parent_window):
  4. parent_window.destroy() # 销毁主界面
  5. self.window = tk.Tk() # 初始框的声明
  6. self.window.title('学生登陆')
  7. self.window.geometry('300x450') # 这里的乘是小x
  8. label = tk.Label(self.window, text='学生登陆', bg='green', font=('Verdana', 20), width=30, height=2)
  9. label.pack()
  10. Label(self.window, text='账号:', font=tkFont.Font(size=14)).pack(pady=25)
  11. self.u_id = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  12. self.u_id.pack()
  13. Label(self.window, text='密码:', font=tkFont.Font(size=14)).pack(pady=25)
  14. self.u_password = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory', show='*')
  15. self.u_password.pack()
  16. Button(self.window, text="登陆", width=8, font=tkFont.Font(size=12), command=self.login).pack(pady=40)
  17. Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back).pack()
  18. self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击
  19. self.window.mainloop() # 进入消息循环
  20. def login(self):
  21. stu_pass = None
  22. # 数据库操作 查询管理员表
  23. db = pymssql.connect(server="localhost",database="LibraryManage", charset='utf8') # 打开数据库连接
  24. cursor = db.cursor() # 使用cursor()方法获取操作游标
  25. sql = "SELECT * FROM Users WHERE u_id = '%s'" % (self.u_id.get()) # SQL 查询语句
  26. try:
  27. # 执行SQL语句
  28. cursor.execute(sql)
  29. # 获取所有记录列表
  30. u_password = None
  31. results = cursor.fetchall()
  32. for row in results:
  33. u_id = row[0]
  34. u_password = row[1]
  35. u_Admin = row[2]
  36. u_name = row[3].encode("l1").decode("GBK")
  37. except:
  38. messagebox.showinfo('警告!', '用户名或密码不正确!')
  39. db.close() # 关闭数据库连接
  40. if self.u_password.get() == u_password:
  41. UserMenu(self.window, u_id, u_name,u_Admin) # 进入学生信息查看界面
  42. else:
  43. messagebox.showinfo('警告!', '用户名或密码不正确!')
  44. def back(self):
  45. StartPage(self.window) # 显示主窗口 销毁本窗口

用户界面

  1. #学生界面
  2. class UserMenu:
  3. def __init__(self, parent_window, u_id, u_name, Admin = 0):
  4. parent_window.destroy() # 销毁主界面
  5. self.u_id = u_id
  6. self.u_name = u_name
  7. self.Admin = Admin
  8. self.window = Tk() # 初始框的声明
  9. self.window.title('用户操作界面')
  10. #———————————————————————————————————————————————————————————中心列表区域———————————————————————————————————————————
  11. self.frame_center = tk.Frame(width=800, height=400)
  12. # 定义下方中心列表区域
  13. self.center_title = Label(self.frame_center, text="借阅的书", font=('Verdana', 20))
  14. self.center_title.grid(row = 0) # 位置设置
  15. self.columns = ("图书编号", "书名", "作者", "出版社", "借阅时间")
  16. self.tree = ttk.Treeview(self.frame_center, show="headings", height=18, columns=self.columns)
  17. self.vbar = ttk.Scrollbar(self.frame_center, orient=VERTICAL, command=self.tree.yview)
  18. # 定义树形结构与滚动条
  19. self.tree.configure(yscrollcommand=self.vbar.set)
  20. # 表格的标题
  21. self.tree.column("图书编号", width=100, anchor='center') # 表示列,不显示
  22. self.tree.column("书名", width=200, anchor='center')
  23. self.tree.column("作者", width=100, anchor='center')
  24. self.tree.column("出版社", width=100, anchor='center')
  25. self.tree.column("借阅时间", width=200, anchor='center')
  26. # 调用方法获取表格内容插入
  27. self.tree.grid(row=1, column=0, sticky=NSEW)
  28. self.vbar.grid(row=1, column=1, sticky=NS)
  29. self.book_id = []
  30. self.book_name = []
  31. self.auther = []
  32. self.press = []
  33. self.lend_time = []
  34. # 打开数据库连接
  35. try:
  36. db = pymssql.connect(server="localhost",database="LibraryManage")
  37. cursor = db.cursor() # 使用cursor()方法获取操作游标
  38. sql = """
  39. SELECT Books.book_id, book_name, author, press, lend_time
  40. FROM Books, Lending
  41. WHERE u_id = %s AND
  42. Lending.book_id = Books.book_id
  43. """%(self.u_id)
  44. cursor.execute(sql)
  45. results = cursor.fetchall()
  46. self.lendable = len(results)
  47. for row in results:
  48. self.book_id.append(row[0])
  49. self.book_name.append(row[1].encode("l1").decode("GBK"))
  50. self.auther.append(row[2].encode("l1").decode("GBK"))
  51. self.press.append(row[3].encode("l1").decode("GBK"))
  52. self.lend_time.append(row[4])
  53. db.close()# 关闭数据库连接
  54. except:
  55. messagebox.showinfo('警告!', '数据库连接失败!')
  56. # 写入数据
  57. n = len(self.book_id)
  58. for i in range(n):
  59. self.tree.insert('', i, values=(self.book_id[i], self.book_name[i], self.auther[i], self.press[i], self.lend_time[i]))
  60. for col in self.columns: # 绑定函数,使表头可排序
  61. self.tree.heading(col, text=col,\
  62. command=lambda _col=col: self.tree_sort_column(self.tree, _col, False))
  63. self.tree.bind('<Button-1>', self.click)
  64. self.frame_center.grid(row=1, column=0, columnspan=2, padx=4, pady=5)
  65. self.frame_center.grid_propagate(0)
  66. self.frame_center.tkraise() # 开始显示主菜单
  67. #————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  68. #——————————————————————————————————————————————————————————左侧借书区域—————————————————————————————————————————————————————
  69. self.frame_left_top = tk.Frame(width=300, height=200)
  70. self.top_title = Label(self.frame_left_top, text="用户信息:", font=('Verdana', 20))
  71. self.top_title.grid(row=0, columnspan=2, sticky=NSEW, padx=50, pady=10)
  72. # u_id
  73. self.left_top_u_id = Label(self.frame_left_top, text="用户编号:" + self.u_id, font=('Verdana', 15))
  74. self.left_top_u_id.grid(row=1) # 位置设置
  75. # u_name
  76. self.left_top_u_name = Label(self.frame_left_top, text="姓名:" +self.u_name , font=('Verdana', 15))
  77. self.left_top_u_name.grid(row=2) # 位置设置
  78. # button
  79. self.left_top_button = ttk.Button(self.frame_left_top, text='借书', width=20, \
  80. command=lambda: Borrow(self.window, self.u_id, self.u_name, self.lendable, self.Admin))
  81. self.left_top_button.grid(row=3, padx=20, pady=10)
  82. self.frame_left_top.grid(row=0, padx=2, pady=5)
  83. self.frame_left_top.grid_propagate(0)
  84. self.frame_left_top.tkraise() # 开始显示主菜单
  85. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  86. #————————————————————————————————————————————————————————右侧还书区域—————————————————————————————————————————————————————————
  87. self.frame_right_top = tk.Frame(width=400, height=200)
  88. self.var_id = StringVar() # 声明书号
  89. self.var_name = StringVar()
  90. # 书编号
  91. self.right_top_bid_label = Label(self.frame_right_top, text="书编号:", font=('Verdana', 15))
  92. self.right_top_bid_entry = Entry(self.frame_right_top, textvariable=self.var_id, font=('Verdana', 15))
  93. self.right_top_bid_label.grid(row=0, column=0) # 位置设置
  94. self.right_top_bid_entry.grid(row=0, column=1)
  95. # 书名
  96. self.right_top_bname_label = Label(self.frame_right_top, text="书名:", font=('Verdana', 15))
  97. self.right_top_bname_entry = Entry(self.frame_right_top, textvariable=self.var_name, font=('Verdana', 15))
  98. self.right_top_bname_label.grid(row=1, column=0) # 位置设置
  99. self.right_top_bname_entry.grid(row=1, column=1)
  100. #还书操作
  101. self.right_top_button = ttk.Button(self.frame_right_top, text='还书', width=20, command = self.delete)
  102. self.right_top_button.grid(row=2, padx=20, pady=10)
  103. if Admin:
  104. self.right_top_button = ttk.Button(self.frame_right_top, text='管理用户', width=20, command = lambda:Users(self.window))
  105. self.right_top_button.grid(row=3, padx=20, pady=10)
  106. self.right_top_button = ttk.Button(self.frame_right_top, text='查询记录', width=20, command = lambda:Records(self.window))
  107. self.right_top_button.grid(row=3,column = 1, padx=20, pady=10)
  108. self.frame_right_top.grid(row=0, column=1, padx=30, pady=30)
  109. self.frame_right_top.grid_propagate(0)
  110. self.frame_right_top.tkraise() # 开始显示主菜单
  111. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  112. self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击
  113. self.window.mainloop() # 进入消息循环
  114. def click(self, event):
  115. self.col = self.tree.identify_column(event.x) # 列
  116. self.row = self.tree.identify_row(event.y) # 行
  117. self.row_info = self.tree.item(self.row, "values")
  118. self.var_id.set(self.row_info[0])
  119. self.var_name.set(self.row_info[1])
  120. self.right_top_bid_entry = Entry(self.frame_right_top, textvariable=self.var_id, font=('Verdana', 15))
  121. self.right_top_bname_entry = Entry(self.frame_right_top, textvariable=self.var_name, font=('Verdana', 15))
  122. def back(self):
  123. StartPage(self.window) # 显示主窗口 销毁本窗口
  124. def delete(self):
  125. res = messagebox.askyesnocancel('确认还书', "是否归还此书:《%s》 ?" %(self.var_name.get()))
  126. if res == True:
  127. try:
  128. db = pymssql.connect(server="localhost",database="LibraryManage", charset='utf8') # 打开数据库连接
  129. cursor = db.cursor() # 使用cursor()方法获取操作游标
  130. sql = "DELETE FROM Lending WHERE u_id = '%s' AND book_id = '%s'" %(self.u_id, self.var_id.get()) # SQL DELETE语句
  131. cursor.execute(sql) # 执行sql语句
  132. db.commit() # 提交到数据库执行
  133. messagebox.showinfo('提示!', '归还成功!')
  134. db.close() # 关闭数据库连接
  135. UserMenu(self.window, self.u_id, self.u_name, self.Admin)
  136. except:
  137. messagebox.showinfo('警告!', '更新失败,数据库连接失败!')

借书界面

  1. class Borrow:
  2. def __init__(self,parent_window, u_id, u_name, num, Admin):
  3. parent_window.destroy() # 销毁主界面
  4. self.num = num
  5. self.u_id = u_id
  6. self.u_name = u_name
  7. self.Admin = Admin
  8. self.window = Tk() # 初始框的声明
  9. self.window.title('借书')
  10. #———————————————————————————————————————————————————————————中心列表区域———————————————————————————————————————————
  11. self.frame_center = tk.Frame(width=800, height=400)
  12. # 定义下方中心列表区域
  13. self.center_title = Label(self.frame_center, text="选择图书", font=('Verdana', 20))
  14. self.center_title.grid(row = 0) # 位置设置
  15. self.columns = ("图书编号", "书名", "作者", "出版社", "剩余数量")
  16. self.tree = ttk.Treeview(self.frame_center, show="headings", height=18, columns=self.columns)
  17. self.vbar = ttk.Scrollbar(self.frame_center, orient=VERTICAL, command=self.tree.yview)
  18. # 定义树形结构与滚动条
  19. self.tree.configure(yscrollcommand=self.vbar.set)
  20. # 表格的标题
  21. self.tree.column("图书编号", width=100, anchor='center') # 表示列,不显示
  22. self.tree.column("书名", width=200, anchor='center')
  23. self.tree.column("作者", width=100, anchor='center')
  24. self.tree.column("出版社", width=100, anchor='center')
  25. self.tree.column("剩余数量", width=100, anchor='center')
  26. # 调用方法获取表格内容插入
  27. self.tree.grid(row=1, column=0, sticky=NSEW)
  28. self.vbar.grid(row=1, column=1, sticky=NS)
  29. self.book_id = []
  30. self.book_name = []
  31. self.auther = []
  32. self.press = []
  33. self.lendable = []
  34. # 打开数据库连接
  35. try:
  36. db = pymssql.connect(server="localhost",database="LibraryManage")
  37. cursor = db.cursor() # 使用cursor()方法获取操作游标
  38. sql = 'SELECT book_id, book_name, author, press, lendable FROM Books'
  39. cursor.execute(sql)
  40. results = cursor.fetchall()
  41. for row in results:
  42. self.book_id.append(row[0])
  43. self.book_name.append(row[1].encode("l1").decode("GBK"))
  44. self.auther.append(row[2].encode("l1").decode("GBK"))
  45. self.press.append(row[3].encode("l1").decode("GBK"))
  46. self.lendable.append(row[4])
  47. db.close()# 关闭数据库连接
  48. except:
  49. print("Error: unable to fetch data")
  50. messagebox.showinfo('警告!', '数据库连接失败!')
  51. # 写入数据
  52. n = len(self.book_id)
  53. for i in range(n):
  54. self.tree.insert('', i, values=(self.book_id[i], self.book_name[i], self.auther[i], self.press[i], self.lendable[i]))
  55. for col in self.columns: # 绑定函数,使表头可排序
  56. self.tree.heading(col, text=col,\
  57. command=lambda _col=col: self.tree_sort_column(self.tree, _col, False))
  58. self.tree.bind('<Button-1>', self.click)
  59. self.frame_center.grid(row=1, column=0, columnspan=2, padx=4, pady=5)
  60. self.frame_center.grid_propagate(0)
  61. self.frame_center.tkraise() # 开始显示主菜单
  62. #————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  63. #——————————————————————————————————————————————————————————左侧借书区域—————————————————————————————————————————————————————
  64. self.frame_left_top = tk.Frame(width=300, height=200)
  65. self.top_title = Label(self.frame_left_top, text="用户信息:", font=('Verdana', 20))
  66. self.top_title.grid(row=0, columnspan=2, sticky=NSEW, padx=50, pady=10)
  67. # u_id
  68. self.left_top_u_id = Label(self.frame_left_top, text="用户编号:" + self.u_id, font=('Verdana', 15))
  69. self.left_top_u_id.grid(row=1) # 位置设置
  70. # u_name
  71. self.left_top_u_name = Label(self.frame_left_top, text="姓名:" +self.u_name , font=('Verdana', 15))
  72. self.left_top_u_name.grid(row=2) # 位置设置
  73. # 可借数量
  74. self.left_top_u_name = Label(self.frame_left_top, text="当前借阅数: %s / 5" %(num) , font=('Verdana', 20))
  75. self.left_top_u_name.grid(row=3) # 位置设置
  76. self.frame_left_top.grid(row=0, padx=2, pady=5)
  77. self.frame_left_top.grid_propagate(0)
  78. self.frame_left_top.tkraise() # 开始显示主菜单
  79. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  80. #————————————————————————————————————————————————————————右侧借书区域—————————————————————————————————————————————————————————
  81. self.frame_right_top = tk.Frame(width=400, height=200)
  82. self.var_id = StringVar() # 声明书号
  83. self.var_name = StringVar()
  84. self.lendable_book = StringVar()
  85. # 书编号
  86. self.right_top_bid_label = Label(self.frame_right_top, text="书编号:", font=('Verdana', 15))
  87. self.right_top_bid_entry = Entry(self.frame_right_top, textvariable=self.var_id, font=('Verdana', 15))
  88. self.right_top_bid_label.grid(row=0, column=0) # 位置设置
  89. self.right_top_bid_entry.grid(row=0, column=1)
  90. # 书名
  91. self.right_top_bname_label = Label(self.frame_right_top, text="书名:", font=('Verdana', 15))
  92. self.right_top_bname_entry = Entry(self.frame_right_top, textvariable=self.var_name, font=('Verdana', 15))
  93. self.right_top_bname_label.grid(row=1, column=0) # 位置设置
  94. self.right_top_bname_entry.grid(row=1, column=1)
  95. #借书操作
  96. self.right_top_button = ttk.Button(self.frame_right_top, text='借书', width=20, command = self.insert)
  97. self.right_top_button.grid(row=2, padx=20, pady=10)
  98. self.frame_right_top.grid(row=0, column=1, padx=30, pady=30)
  99. self.frame_right_top.grid_propagate(0)
  100. self.frame_right_top.tkraise() # 开始显示主菜单
  101. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  102. self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击
  103. self.window.mainloop() # 进入消息循环
  104. def click(self, event):
  105. self.col = self.tree.identify_column(event.x) # 列
  106. self.row = self.tree.identify_row(event.y) # 行
  107. self.row_info = self.tree.item(self.row, "values")
  108. self.var_id.set(self.row_info[0])
  109. self.var_name.set(self.row_info[1])
  110. self.lendable_book.set(self.row_info[4])
  111. self.right_top_bid_entry = Entry(self.frame_right_top, textvariable=self.var_id, font=('Verdana', 15))
  112. self.right_top_bname_entry = Entry(self.frame_right_top, textvariable=self.var_name, font=('Verdana', 15))
  113. def insert(self):
  114. if self.num >= 5:
  115. messagebox.showinfo('提示!', '达到最大借阅数量,请先归还书籍。')
  116. UserMenu(self.window, self.u_id, self.u_name)
  117. return None
  118. if int(self.lendable_book.get()) <= 0:
  119. messagebox.showinfo('提示!', '该书数量不足,换一本书吧!')
  120. return None
  121. res = messagebox.askyesnocancel('确认借书', "是借阅此书:《%s》 ?" %(self.var_name.get()))
  122. if res == True:
  123. try:
  124. db = pymssql.connect(server="localhost",database="LibraryManage", charset='utf8') # 打开数据库连接
  125. cursor = db.cursor() # 使用cursor()方法获取操作游标
  126. sql = "INSERT INTO Records(u_id,book_id) VALUES('%s', '%s')" %(self.u_id, self.var_id.get()) # SQL INSERT语句
  127. cursor.execute(sql) # 执行sql语句
  128. db.commit() # 提交到数据库执行
  129. messagebox.showinfo('提示!', '借阅成功!')
  130. db.close() # 关闭数据库连接
  131. UserMenu(self.window, self.u_id, self.u_name, self.Admin)
  132. except:
  133. db.rollback() # 发生错误时回滚
  134. messagebox.showinfo('警告!', '更新失败,数据库连接失败!')
  135. def back(self):
  136. UserMenu(self.window, self.u_id, self.u_name, self.Admin)

 管理用户

  1. class Users:
  2. def __init__(self,parent_Window):
  3. parent_Window.destroy()
  4. self.window = Tk() # 初始框的声明
  5. self.window.title('管理界面')
  6. #———————————————————————————————————————————————————————————中心列表区域———————————————————————————————————————————
  7. self.frame_center = tk.Frame(width=800, height=400)
  8. # 定义下方中心列表区域
  9. self.center_title = Label(self.frame_center, text="用户信息", font=('Verdana', 20))
  10. self.center_title.grid(row = 0) # 位置设置
  11. self.columns = ("用户ID", "姓名", "密码", "电话", "地址", "管理权限")
  12. self.tree = ttk.Treeview(self.frame_center, show="headings", height=18, columns=self.columns)
  13. self.vbar = ttk.Scrollbar(self.frame_center, orient=VERTICAL, command=self.tree.yview)
  14. # 定义树形结构与滚动条
  15. self.tree.configure(yscrollcommand=self.vbar.set)
  16. # 表格的标题
  17. self.tree.column("用户ID", width=100, anchor='center') # 表示列,不显示
  18. self.tree.column("姓名", width=100, anchor='center')
  19. self.tree.column("密码", width=100, anchor='center')
  20. self.tree.column("电话", width=100, anchor='center')
  21. self.tree.column("地址", width=300, anchor='center')
  22. self.tree.column("管理权限", width=100, anchor='center')
  23. # 调用方法获取表格内容插入
  24. self.tree.grid(row=1, column=0, sticky=NSEW)
  25. self.vbar.grid(row=1, column=1, sticky=NS)
  26. self.u_id = []
  27. self.u_name = []
  28. self.u_password = []
  29. self.user_phone = []
  30. self.user_adress = []
  31. self.u_Admin = []
  32. # 打开数据库连接
  33. try:
  34. db = pymssql.connect(server="localhost",database="LibraryManage")
  35. cursor = db.cursor() # 使用cursor()方法获取操作游标
  36. sql = 'SELECT u_id, u_password, u_name, user_phone, user_adress, u_Admin FROM Users'
  37. cursor.execute(sql)
  38. results = cursor.fetchall()
  39. for row in results:
  40. self.u_id.append(row[0])
  41. self.u_password.append(row[1].encode("l1").decode("GBK"))
  42. self.u_name.append(row[2].encode("l1").decode("GBK"))
  43. self.user_phone.append(row[3].encode("l1").decode("GBK"))
  44. self.user_adress.append(row[4])
  45. if row[5]:
  46. self.u_Admin.append('管理员')
  47. else:
  48. self.u_Admin.append('')
  49. db.close()# 关闭数据库连接
  50. except:
  51. print("Error: unable to fetch data")
  52. messagebox.showinfo('警告!', '数据库连接失败!')
  53. # 写入数据
  54. n = len(self.u_id)
  55. for i in range(n):
  56. self.tree.insert('', i, values=(self.u_id[i], self.u_name[i], self.u_password[i], self.user_phone[i], \
  57. self.user_adress[i], self.u_Admin[i]))
  58. for col in self.columns: # 绑定函数,使表头可排序
  59. self.tree.heading(col, text=col,\
  60. command=lambda _col=col: self.tree_sort_column(self.tree, _col, False))
  61. self.tree.bind('<Button-1>', self.click)
  62. self.frame_center.grid(row=1, column=0, columnspan=2, padx=4, pady=5)
  63. self.frame_center.grid_propagate(0)
  64. self.frame_center.tkraise() # 开始显示主菜单
  65. #————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  66. #————————————————————————————————————————————————————————右侧还书区域—————————————————————————————————————————————————————————
  67. self.frame_right_top = tk.Frame(width=600, height=100)
  68. self.var_id = StringVar() # 声明书号
  69. self.var_name = StringVar()
  70. # 书编号
  71. self.right_top_bid_label = Label(self.frame_right_top, text="用户ID:", font=('Verdana', 15))
  72. self.right_top_bid_entry = Entry(self.frame_right_top, textvariable=self.var_id, font=('Verdana', 15))
  73. self.right_top_bid_label.grid(row=0, column=0) # 位置设置
  74. self.right_top_bid_entry.grid(row=0, column=1)
  75. # 书名
  76. self.right_top_bname_label = Label(self.frame_right_top, text="姓名:", font=('Verdana', 15))
  77. self.right_top_bname_entry = Entry(self.frame_right_top, textvariable=self.var_name, font=('Verdana', 15))
  78. self.right_top_bname_label.grid(row=1, column=0) # 位置设置
  79. self.right_top_bname_entry.grid(row=1, column=1)
  80. #还书操作
  81. self.right_top_button = ttk.Button(self.frame_right_top, text='修改信息', width=20, command = self.update)
  82. self.right_top_button.grid(row=1, column=4, padx=20, pady=10)
  83. self.frame_right_top.grid(row=0, padx=30, pady=30)
  84. self.frame_right_top.grid_propagate(0)
  85. self.frame_right_top.tkraise() # 开始显示主菜单
  86. #——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  87. self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击
  88. self.window.mainloop() # 进入消息循环
  89. def click(self, event):
  90. self.col = self.tree.identify_column(event.x) # 列
  91. self.row = self.tree.identify_row(event.y) # 行
  92. self.row_info = self.tree.item(self.row, "values")
  93. self.var_id.set(self.row_info[0])
  94. self.var_name.set(self.row_info[1])
  95. self.right_top_bid_entry = Entry(self.frame_right_top, textvariable=self.var_id, font=('Verdana', 15))
  96. self.right_top_bname_entry = Entry(self.frame_right_top, textvariable=self.var_name, font=('Verdana', 15))
  97. def update(self):
  98. self.update_window = tk.Tk()
  99. self.update_window.title('修改')
  100. self.update_window.geometry('600x150')
  101. Label(self.update_window, text='设置密码', font=tkFont.Font(size=14)).grid(row = 0, column = 0)
  102. self.new_password = tk.Entry(self.update_window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  103. self.new_password.grid(row = 0, column = 1)
  104. Label(self.update_window, text='设置电话', font=tkFont.Font(size=14)).grid(row = 1, column = 0)
  105. self.new_phone = tk.Entry(self.update_window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  106. self.new_phone.grid(row = 1, column = 1)
  107. Label(self.update_window, text='设置地址', font=tkFont.Font(size=14)).grid(row = 2, column = 0)
  108. self.new_adress = tk.Entry(self.update_window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  109. self.new_adress.grid(row = 2, column = 1)
  110. Label(self.update_window, text='是否设为管理员(0或1)', font=tkFont.Font(size=14)).grid(row = 3, column = 0)
  111. self.new_Admin = tk.Entry(self.update_window, width=30, font=tkFont.Font(size=14), bg='Ivory')
  112. self.new_Admin.grid(row = 3, column = 1)
  113. Button(self.update_window, text="修改", width=40, font=tkFont.Font(size=15), \
  114. command=self.comfirm).grid(row = 4, column = 0, columnspan = 2)
  115. self.update_window.mainloop()
  116. def comfirm(self):
  117. res = messagebox.askyesnocancel('确认修改', "确认修改 ?")
  118. if res:
  119. db = pymssql.connect(server="localhost",database="LibraryManage")
  120. cursor = db.cursor() # 使用cursor()方法获取操作游标
  121. if self.new_password.get() != '':
  122. sql = "UPDATE Users SET u_password = '%s' WHERE u_id = '%s'" %(self.new_password.get(), self.var_id.get())
  123. cursor.execute(sql) # 执行sql语句
  124. db.commit() # 提交到数据库执行
  125. if self.new_phone.get() != '':
  126. sql = "UPDATE Users SET user_phone = '%s' WHERE u_id = '%s'" %(self.new_phone.get(), self.var_id.get())
  127. cursor.execute(sql) # 执行sql语句
  128. db.commit() # 提交到数据库执行
  129. if self.new_adress.get() != '':
  130. sql = "UPDATE Users SET user_adress = '%s' WHERE u_id = '%s'" %(self.new_adress.get(), self.var_id.get())
  131. cursor.execute(sql) # 执行sql语句
  132. db.commit() # 提交到数据库执行
  133. if self.new_Admin.get() != '':
  134. sql = "UPDATE Users SET u_Admin = '%s' WHERE u_id = '%s'" %(self.new_Admin.get(), self.var_id.get())
  135. cursor.execute(sql) # 执行sql语句
  136. db.commit() # 提交到数据库执行
  137. messagebox.showinfo('提示!', '修改成功!')
  138. self.update_window.destroy()
  139. Users(self.window)
  140. def back(self):
  141. StartPage(self.window)

 查询记录

  1. class Records:
  2. def __init__(self,parent_window):
  3. parent_window.destroy()
  4. self.window = Tk() # 初始框的声明
  5. self.window.title('借阅记录')
  6. #———————————————————————————————————————————————————————————中心列表区域———————————————————————————————————————————
  7. self.frame_center = tk.Frame(width=900, height=400)
  8. # 定义下方中心列表区域
  9. self.center_title = Label(self.frame_center, text="借阅记录", font=('Verdana', 20))
  10. self.center_title.grid(row = 0) # 位置设置
  11. self.columns = ("记录ID", "用户ID", "姓名", "书ID", "书名", "借阅日期", "还书日期")
  12. self.tree = ttk.Treeview(self.frame_center, show="headings", height=18, columns=self.columns)
  13. self.vbar = ttk.Scrollbar(self.frame_center, orient=VERTICAL, command=self.tree.yview)
  14. # 定义树形结构与滚动条
  15. self.tree.configure(yscrollcommand=self.vbar.set)
  16. # 表格的标题
  17. self.tree.column("记录ID", width=100, anchor='center')
  18. self.tree.column("用户ID", width=100, anchor='center') # 表示列,不显示
  19. self.tree.column("姓名", width=100, anchor='center')
  20. self.tree.column("书ID", width=100, anchor='center')
  21. self.tree.column("书名", width=100, anchor='center')
  22. self.tree.column("借阅日期", width=200, anchor='center')
  23. self.tree.column("还书日期", width=200, anchor='center')
  24. # 调用方法获取表格内容插入
  25. self.tree.grid(row=1, column=0, sticky=NSEW)
  26. self.vbar.grid(row=1, column=1, sticky=NS)
  27. self.u_id = []
  28. self.u_name = []
  29. self.book_id = []
  30. self.book_name = []
  31. self.Record_id = []
  32. self.lend_time = []
  33. self.return_time = []
  34. # 打开数据库连接
  35. try:
  36. db = pymssql.connect(server="localhost",database="LibraryManage")
  37. cursor = db.cursor() # 使用cursor()方法获取操作游标
  38. sql = """
  39. SELECT Record_id, Users.u_id, u_name, Books.book_id, book_name, lend_time, return_time
  40. FROM Records,Users,Books
  41. WHERE Records.u_id = Users.u_id AND
  42. Books.book_id = Records.book_id
  43. """
  44. cursor.execute(sql)
  45. results = cursor.fetchall()
  46. for row in results:
  47. self.Record_id.append(row[0])
  48. self.u_id.append(row[1])
  49. self.u_name.append(row[2].encode("l1").decode("GBK"))
  50. self.book_id.append(row[3])
  51. self.book_name.append(row[4].encode("l1").decode("GBK"))
  52. self.lend_time.append(row[5])
  53. self.return_time.append(row[6])
  54. db.close()# 关闭数据库连接
  55. except:
  56. print("Error: unable to fetch data")
  57. messagebox.showinfo('警告!', '数据库连接失败!')
  58. # 写入数据
  59. n = len(self.u_id)
  60. for i in range(n):
  61. self.tree.insert('', i, values=(self.Record_id[i], self.u_id[i], self.u_name[i], self.book_id[i], \
  62. self.book_name[i], self.lend_time[i],self.return_time[i]))
  63. for col in self.columns: # 绑定函数,使表头可排序
  64. self.tree.heading(col, text=col,\
  65. command=lambda _col=col: self.tree_sort_column(self.tree, _col, False))
  66. self.frame_center.grid(row=1, column=0, columnspan=2, padx=4, pady=5)
  67. self.frame_center.grid_propagate(0)
  68. self.frame_center.tkraise() # 开始显示主菜单
  69. self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击
  70. self.window.mainloop() # 进入消息循环
  71. def back(self):
  72. StartPage(self.window) # 显示主窗口 销毁本窗口
  73. if __name__ == '__main__':
  74. window = tk.Tk()
  75. StartPage(window)

 总体就是这样,将以上所有的python代码全放到一起就可以运行,想要运行首先要构建数据库,并打开,还要安装包,需要全部内容的例如PPT,可以加我QQ1486426078

补充:直接去网盘下载吧,过了两年了我已经忘完了,安装运行什么的自己研究吧,嘿嘿。链接: https://pan.baidu.com/s/1k-heOKsYFUg2H0CeNIsVPQ?pwd=1234 提取码: 1234 复制这段内容后打开百度网盘手机App,操作更方便哦

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/859514
推荐阅读
相关标签
  

闽ICP备14008679号