Home > OS >  Search in database and in dictionary in relation to the choice of the database, in order to satisfy
Search in database and in dictionary in relation to the choice of the database, in order to satisfy

Time:10-14

I would like to use the contents of the combobox to search the database and dictionary. For example:

  • I select John in the combobox (or any name, without specifying a certain name)
  • If in the Table1 database there is a John (in the name column) who is 18 years old in column Age
  • And if in the dictionary John has Great in Talent
  • Then I print "ok". The above two conditions must both be satisfied.

In the code I don't want to specify John, but any element of the combobox. I specify that John is both in the database and in the dictionary: there is both the name column in the database and name in the dictionary (they are two different things). I use SQLite

PROBLEM: The problem is that I print "ok" with whatever name I select in the combobox and this is incorrect. For example "Richard" is present in the combobox but is not present in the database, but "ok" is printed anyway and this is not correct. Also, there is a problem with the dictionary too, but I don't understand why.

I report only the part of the code useful for the resolution (without form tkinter settings and database setting). Thank you

from tkinter import ttk
import tkinter as tk
from tkinter import *
import sqlite3

root = tk.Tk()
root.geometry("350x200")

con = sqlite3.connect('/home/linux/Desktop/test2.db')
cursor = con.cursor()

name = {"John": {"Talent": "Great"},
        "Michael": {"Talent": "Average"},
       } #reductive example

combo1=ttk.Combobox(root, width = 22)
combo1['value'] =["John", "Richard", "Frank"] #and other 17 name
combo1.place(x=10, y=10)
combo1.set("Select")

def my_function():
    if  (cursor.execute("SELECT 1 FROM Table1 WHERE age = 18 AND name = ?", (combo1.get(),))) \
    and teams[combo1.get()]["Talent"]:
        print("ok")

btn = Button(root, text="Click", command = my_function)
btn.place(x=10, y=50)

CodePudding user response:

I am assuming you mean name, not teams in my_function? At the moment you are only checking if name['someone']['Talent'] is not empty, not whether it meets your talent criterion. I introduced the variable talent, you have to adapt the code to your needs. Your issue printing always 'Ok' was already adressed elsewhere

def my_function():
    cursor.execute("SELECT 1 FROM Table1 WHERE age = 18 AND name = ?", (combo1.get(),))
    if cursor.fetchall() and (name[combo1.get()]["Talent"] == "Great"):
        print("ok")

  • Related