如何使用用户输入不区分大小写进行python搜索

时间:2014-03-03 12:16:49

标签: python tkinter case-sensitive

嗨我有一个很好的python搜索,然后我有一个接受用户输入的tkinter GUI。然后使用用户输入来使用python查询数据库。它根据输入将记录匹配在一起,例如当我需要它以匹配蓝色到蓝色等时它会匹配蓝色到蓝色等。有人知道如何做到这一点,这里是我已经拥有的一个例子(注意它是一个制作方案);

def Search():
global E1, E2, E3, E4, file


    #Open the file in read mode
    file = sqlite3.connect('H:\\matching.db')

    #Welcome message
    print ("Please type in the information required")


    window = Tk()
    window.title ("Search for matches")

    def QuitSearch():
       window.destroy()


   #Create text that will be shown to the user
   window.configure(bg="red")
   Label(window, text="Please enter the colour of your hair").grid(row=0)
   Label(window, text="Please enter the colour of your eyes").grid(row=1)
   Label(window, text="Please enter your gender").grid(row=2)
   Label(window, text="Please enter your shoe size").grid(row=3)


   #Create where the user will input
   E1= Entry(window)
   E2= Entry(window)
   E3= Entry(window)
   E4= Entry(window)

   #Assigning the input boxes to area on the screen
   E1.grid(row=0, column=1)
   E2.grid(row=1, column=1)
   E3.grid(row=2, column=1)
   E4.grid(row=3, column=1)

   button = Button(window, text = "Submit information", command=Submit, fg="yellow", bg="black")
   button.grid(row=3, column=2, padx = 5)


   quitbutton = Button (window, text ="QUIT", command=QuitSearch, fg ="red")
   quitbutton.grid(row=3, column=3, padx=5)







#The submit function allows the data inserted by the user in Search to be submitted and to search the database    
def Submit():
    global E1, E2, E3, E4, file

    #Retaining user input
    eyecolour = E1.get()
    haircolour = E2.get()
    gender = E3.get()
    shoesize = E4.get()



    #The search
    cursor = file.execute ('''SELECT ID, forename, surname, FROM people
    WHERE eyecolour =? and haircolour=? and gender=? and shoesize=? ''', (eyecolour, haircolour, gender, shoezize)) 

    window=Tk()
    def QuitOutputScreen():
    window.destroy()

    for row_number, row in enumerate(cursor):
        Label(window, text ="ID = "+ str(row[0])).grid(row=1, column = row_number)
        Label(window, text ="Forename = "+str(row[1])).grid(row=2, column = row_number)
        Label(window, text ="Surname = "+(row[2])).grid(row=3, column = row_number)


    Label(window, text ="Search complete ").grid(column=11)

    quitbutton = Button (window, text ="QUIT", command=QuitOutputScreen, fg ="red")
    quitbutton.grid(row=11, column=11, padx=5)


file.close()

2 个答案:

答案 0 :(得分:1)

这看起来更像是一个涉及DBMS(在本例中为sqlite)的问题,而不是Python本身。

您可以在sqlite中使用不区分大小写的搜索查询来解决问题。您可以在How to set Sqlite3 to be case insensitive when string comparing?找到一些解释。

答案 1 :(得分:0)

在你的python代码中,你可以使用所有小写或大写来存储和搜索,使用字符串lower和upper函数,或者你可以使用正则表达式和re.IGNORE_CASE来进行搜索。

对于数据库查询,您可以将所有内容存储在固定的大小写中,也可以告诉数据库引擎忽略大小写。