翼度科技»论坛 编程开发 python 查看内容

数据库实验五:数据库编程

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
2、设计一个小型的数据库应用程序 可利用现有的数据库,也可重新设计数据库。 要求实现数据的增加、删除、修改、查询的功能。 在报告中描述清楚使用的数据库、数据表及实现的功能(要求截图,并附代码)
  1. 设计一个小型的数据库应用程序
  2. 数据库名:student
  3. 表名:infor
  4. 字段:
  5.   Sno:学号
  6.   Sname:姓名
  7.   Ssex:性别
  8.   Sage:年龄
  9.   Smaj:专业
  10.   Srew:奖项
  11. 功能实现
  12. 1. 添加学生信息:向表中插入学生的学号、姓名、性别、年龄、专业、奖项。
  13. 2. 修改学生信息:根据学号修改学生的姓名、性别、年龄、专业、奖项。
  14. 3. 删除学生信息:根据学号删除学生的所有信息。
  15. 4. 查询学生信息:根据学号查询学生的所有信息。
  16.    程序实现(Python连接MySQL)
  17. import pymysql  # python连接mysql的驱动
  18. import tkinter as tk  # 图形化界面的模块
  19. import tkinter.ttk as ttk
  20. import tkinter.messagebox  # 要使用messagebox先要导入模块
  21. table = "infor"
  22. # 建立连接,在每个按钮的函数里创建游标
  23. db = pymysql.connect(host="192.168.163.241", user="root", passwd="123456", db="student", port=3306, charset='utf8')
  24. # 清空输入框内容
  25. def clearAll():
  26.     entryId.delete(0, 'end')
  27.     entryName.delete(0, 'end')
  28.     entryMaj.delete(0, 'end')
  29.     entryRew.delete(0, 'end')
  30.     spinboxSex.delete(0, 'end')
  31.     spinboxAge.delete(0, 'end')
  32. # 根据学号进行查询
  33. def search():
  34.     cursor = db.cursor()
  35.     student_id = entryId.get()
  36.     if student_id != '':
  37.         try:
  38.             student_id = int(student_id)
  39.             sql = "SELECT *, Srew FROM {} WHERE Sno = {}".format(table, student_id)
  40.             cursor.execute(sql)
  41.             result = cursor.fetchone()
  42.             if result is not None:
  43.                 data = list(result)
  44.                 tk.messagebox.showinfo(title='Info', message=tuple(data))
  45.             else:
  46.                 tk.messagebox.showerror(title='错误!', message='查无此人!请重新输入!')
  47.         except:
  48.             tk.messagebox.showerror(title='错误!', message='输入错误!请重新输入!')
  49.     clearAll()
  50.     cursor.close()
  51. # 修改 信息
  52. def alter():
  53.     cursor = db.cursor()
  54.     id = entryId.get()
  55.     name = entryName.get()
  56.     sex = spinboxSex.get()
  57.     age = spinboxAge.get()
  58.     maj = entryMaj.get()
  59.     rew = entryRew.get()
  60.     if (id != ''):
  61.         try:
  62.             id = int(id)
  63.             sql = "select * from {} where Sno = {}".format(table, id)
  64.             cursor.execute(sql)
  65.             if (cursor.fetchone() != None):
  66.                 sql1 = "update infor set Sname = '{}', Ssex = '{}', Sage = {}, Smaj = '{}', Srew = '{}' where Sno = {}".format(
  67.                     name, sex, age, maj, rew, id)
  68.                 cursor.execute(sql1)
  69.                 tk.messagebox.showinfo(title='Info', message='修改成功')
  70.             else:
  71.                 tk.messagebox.showerror(title='错误!', message='查无此人!请重新输入!')
  72.         except:
  73.             tk.messagebox.showerror(title='错误!', message='输入错误!请重新输入!')
  74.     else:
  75.         tk.messagebox.showerror(title='错误!', message='学号不能为空!请重新输入!')
  76.     db.commit()
  77.     clearAll()
  78.     cursor.close()
  79. # 根据学号 进行删除
  80. def delete():
  81.     cursor = db.cursor()
  82.     id = entryId.get()
  83.     if (id != ''):
  84.         try:
  85.             id = int(id)
  86.             sql = "select * from {} where Sno = {}".format(table, id)
  87.             cursor.execute(sql)
  88.             if (cursor.fetchone() != None):
  89.                 sql1 = "delete from {} where Sno = {}".format(table, id)
  90.                 cursor.execute(sql1)
  91.                 sql2 = "delete from {} where Sno = {}".format(table, id)
  92.                 cursor.execute(sql2)
  93.                 tk.messagebox.showinfo(title='Info', message='删除成功')
  94.             else:
  95.                 tk.messagebox.showerror(title='错误!', message='查无此人!请重新输入!')
  96.         except:
  97.             tk.messagebox.showerror(title='错误!', message='输入错误!请重新输入!')
  98.     else:
  99.         tk.messagebox.showerror(title='错误!', message='学号不能为空!请重新输入!')
  100.     db.commit()
  101.     clearAll()
  102.     cursor.close()
  103. # !!!python对MySQL进行数据的插入、更新和删除之后需要commit,数据库才会真的有数据操作。插入内容
  104. def insert():
  105.     cursor = db.cursor()
  106.     id = entryId.get()
  107.     name = entryName.get()
  108.     sex = spinboxSex.get()
  109.     age = spinboxAge.get()
  110.     maj = entryMaj.get()
  111.     rew = entryRew.get()
  112.     if (id != ''):
  113.         id = int(id)
  114.         sql = "select * from {} where Sno = {}".format(table, id)
  115.         cursor.execute(sql)
  116.         if (cursor.fetchone() != None):
  117.             tk.messagebox.showerror(title='错误!', message='已有此人!请重新输入!')
  118.         else:
  119.             sql1 = "insert into infor (Sno, Sname, Ssex, Sage, Smaj, Srew) values({}, "{}", "{}", {}, "{}", "{}")".format(
  120.                 id, name, sex, age, maj, rew)
  121.             cursor.execute(sql1)
  122.             tk.messagebox.showinfo(title='Info', message='添加成功!')
  123.     else:
  124.         tk.messagebox.showerror(title='错误!', message='学号不能为空!请重新输入!')
  125.     db.commit()
  126.     clearAll()
  127.     cursor.close()
  128. # 控件的布局
  129. windows = tk.Tk()
  130. windows.title('学生信息管理')
  131. # 第1行控件
  132. lblId = tk.Label(text='学号:')
  133. lblId.grid(row=0, column=0)
  134. entryId = tk.Entry()
  135. entryId.grid(row=0, column=1)
  136. lblName = tk.Label(text='姓名:')
  137. lblName.grid(row=0, column=2)
  138. entryName = tk.Entry()
  139. entryName.grid(row=0, column=3)
  140. # 第2行控件
  141. lblSex = tk.Label(text='性别:')
  142. lblSex.grid(row=1, column=0)
  143. spinboxSex = tk.Spinbox(windows, value=('男', '女'))
  144. spinboxSex.grid(row=1, column=1)
  145. lblAge = tk.Label(text='年龄:')
  146. lblAge.grid(row=1, column=2)
  147. spinboxAge = tk.Spinbox(windows, from_=15, to=40)
  148. spinboxAge.grid(row=1, column=3)
  149. # 第3行控件
  150. lblMaj = tk.Label(text='专业:')
  151. lblMaj.grid(row=2, column=0)
  152. entryMaj = tk.Entry()
  153. entryMaj.grid(row=2, column=1)
  154. lblRew = tk.Label(text='奖励:')
  155. lblRew.grid(row=2, column=2)
  156. entryRew = tk.Entry()
  157. entryRew.grid(row=2, column=3)
  158. # 分割线
  159. ttk.Separator(orient=tk.HORIZONTAL).grid(row=3, column=0, columnspan=6, pady=10, sticky=tk.W + tk.E)
  160. # 按钮控件
  161. btnSer = tk.Button(text='查询', command=search)
  162. btnSer.grid(row=4, column=0)
  163. btnIdx = tk.Button(text='插入', command=insert)
  164. btnIdx.grid(row=4, column=1)
  165. btnRep = tk.Button(text='修改', command=alter)
  166. btnRep.grid(row=4, column=2)
  167. btnDel = tk.Button(text='删除', command=delete)
  168. btnDel.grid(row=4, column=3)
  169. windows.mainloop()
复制代码
  

来源:https://www.cnblogs.com/youxiandechilun/p/18251547
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具