def selectself db = pymysqlconnecthost=localhost user=root password=111111 port=3306 db=student charset=utf8 selfcursor = dbcursor sql = fSELECT FROM pigherd WHERE herdid=selfherdid_t OR her
假设你是在使用tkinter创建一个窗口,你可以尝试以下代码来实现查询数据并显示到窗口中:
import tkinter as tk
import pymysql
class MyApp:
def __init__(self):
self.window = tk.Tk()
self.herdid_t = tk.StringVar()
self.herdname_t = tk.StringVar()
self.creattime_t = tk.StringVar()
tk.Label(self.window, text="herdid").grid(row=0, column=0)
tk.Label(self.window, text="herdname").grid(row=1, column=0)
tk.Label(self.window, text="creattime").grid(row=2, column=0)
self.herdid_entry = tk.Entry(self.window, textvariable=self.herdid_t)
self.herdid_entry.grid(row=0, column=1)
self.herdname_entry = tk.Entry(self.window, textvariable=self.herdname_t)
self.herdname_entry.grid(row=1, column=1)
self.creattime_entry = tk.Entry(self.window, textvariable=self.creattime_t)
self.creattime_entry.grid(row=2, column=1)
self.select_button = tk.Button(self.window, text="查询", command=self.select)
self.select_button.grid(row=3, columnspan=2)
self.result_label = tk.Label(self.window, text="")
self.result_label.grid(row=4, columnspan=2)
def select(self):
db = pymysql.connect(host='localhost', user='root', password='111111', port=3306, db='student', charset='utf8')
cursor = db.cursor()
sql = f"SELECT * FROM pigherd WHERE herdid='{self.herdid_t.get()}' OR herdname='{self.herdname_t.get()}' OR creattime='{self.creattime_t.get()}'"
cursor.execute(sql)
result = cursor.fetchone()
self.result_label.config(text=result)
db.close()
def run(self):
self.window.mainloop()
app = MyApp()
app.run()
在这个代码中,我们创建了一个简单的窗口,并添加了三个输入框和一个查询按钮。当点击查询按钮时,调用select方法,在方法中执行查询操作,并将结果显示在result_label标签中
原文地址: https://www.cveoy.top/t/topic/h4l8 著作权归作者所有。请勿转载和采集!