python - 连接到本地主机数据库的 python 中的搜索框

标签 python mysql sql tkinter

大家好我正在为我的学校开发一个项目,在同一个项目中我需要一个 python 搜索框,我已经或多或少地知道它是如何开发的,但它给出了一些错误。抱歉我的英语不好,但这不是我的母语。 我在这里留下我的代码。

import mysql.connector
from tkinter import *
con = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="",
    database="testing"
)
top = Tk()
top.title('Search form')
top.geometry("200x200")
cursor = con.cursor()
name_box = Entry()
name_box.pack(side = RIGHT, expand = True)


def search(tup):
    try:
        person_name = name_box.get()
        cursor.execute("SELECT * FROM `admin` WHERE `email`=%s",name.box.get())
        return (cursor.fetchone())
    


        self.button = Button(self.frame, text="Display text", command=search)

最佳答案

您的代码缺少一个重要部分,请参阅末尾的最后一个命令,以便您可以输入任意数量的搜索

你走在正确的轨道上,但如你所见,mysql 需要更多行

import mysql.connector
import datetime
from mysql.connector.cursor import MySQLCursorPrepared
from tkinter import *

mydb = mysql.connector.connect(
     host="localhost",
     user="root",
     passwd="password",
     database="testdb"
)
mycursor = mydb.cursor(cursor_class=MySQLCursorPrepared)
top = Tk()
top.title('Search form')
top.geometry("200x200")
name_box = Entry()
name_box.pack(side = RIGHT, expand = True)

def get_name():
    global person_name
    person_name = name_box.get()
    try:
        sql1="SELECT `mytable`.`Name` FROM `testdb`.`mytable` WHERE Name = %s;"
        mycursor.execute(sql1,(person_name,))
        records = mycursor.fetchall()
        for x in records:
            print(x)
    except mysql.connector.Error as err:
        print("Something went wrong: {}".format(err))

person_name = ""
button = Button(top, text="Display text", command=get_name)
button.pack()


mainloop()

关于python - 连接到本地主机数据库的 python 中的搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62796963/

相关文章:

python - 从末尾到开头切片字符串

mysql - SQL 选择语句

mysql - SQL如何获取最便宜的33%产品的最高价格

mysql - 根据另一个表 ID 计算表行数

sql - 错误 : The used SELECT statements have a different number of columns

sql - 如何在第二个连接表中进行多个条件的连接?

mysql - MySQL 中何时使用单引号、双引号和反引号

python - 用 Python 编码日语

python - 大于缓冲页 1/3 的值无法编入索引 Django 错误

python - Django 在删除模型之前检查模型是否有相关对象