def look_row(self): db = pymysql.connect(host='localhost', port=3306, db='crime', user='qingying', password='123456') cursor = db.cursor() # 获取输入框信息 id_value = self.right_top_id_entry.get() name_value = self.right_top_name_entry.get() sex_value = self.right_top_sex_entry.get() cause_value = self.right_top_cause_entry.get() time1_value = self.right_top_cause_entry.get() time2_value = self.right_top_cause_entry.get() nei_value = self.right_top_nei_entry.get() lao_value = self.right_top_lao_entry.get() tai_value = self.right_top_tai_entry.get() # 拼接SQL语句 sql = "SELECT * FROM criminal WHERE " if id_value: sql += f"id='{id_value}' AND " if name_value: sql += f"name='{name_value}' AND " if sex_value: sql += f"sex='{sex_value}' AND " if cause_value: sql += f"cause='{cause_value}' AND " if time1_value: sql += f"time1='{time1_value}' AND " if time2_value: sql += f"time2='{time2_value}' AND " if nei_value: sql += f"nei='{nei_value}' AND " if lao_value: sql += f"lao='{lao_value}' AND " if tai_value: sql += f"tai='{tai_value}' AND " sql = sql[:-5] try: cursor.execute(sql) results = cursor.fetchall() for record in self.tree.get_children(): self.tree.delete(record) for row in results: self.tree.insert('', tk.END, values=row) except: messagebox.showinfo('警告!', '查询失败,数据库连接失败!') db.close() self.right_top_id_entry.delete(0, tk.END) self.right_top_name_entry.delete(0, tk.END) self.right_top_sex_entry.delete(0, tk.END) self.right_top_cause_entry.delete(0, tk.END) self.right_top_time1_entry.delete(0, tk.END) self.right_top_time2_entry.delete(0, tk.END) self.right_top_nei_entry.delete(0, tk.END) self.right_top_lao_entry.delete(0, tk.END) self.right_top_tai_entry.delete(0, tk.END)
时间: 2024-04-01 15:37:19 浏览: 106
django.db.utils.ProgrammingError: (1146, u“Table‘’ doesn’t exist”)问题的解决
这段代码是一个GUI程序的部分代码,实现了根据用户输入的条件查询数据库表中的数据,并将查询结果显示在GUI界面上的表格中。
具体实现过程是先获取用户在GUI界面上输入的查询条件,然后根据这些条件构建SQL查询语句。如果用户没有输入某个查询条件,则不将该条件加入SQL语句中。接着使用pymysql连接数据库,执行SQL查询语句,并将查询结果显示在GUI界面上的表格中。如果查询失败,则会弹出一个警告框提示用户。
最后,清空输入框中的内容,以便用户进行下一次查询。
阅读全文