I'm unable to solve a problem with a search query in the database (sqlite3) in Tkinter. Parts of my code:
front.py
# Entries self.name_text = tk.StringVar() self.entry_name = tk.Entry(self.parent, textvariable=self.name_text) self.entry_name.grid(row=3, column=1) self.color_text = tk.StringVar() self.combobox2=ttk.Combobox(self.parent, textvariable=self.color_text) self.combobox2["values"] = ('red','blue','white') self.labelCombobox=ttk.Label(self.parent, textvariable=self.color_text) self.combobox2.grid(row=4, column=1) self.parent.bind('<Return>',lambda e:refresh()) def search_command(self): self.listBox.delete(0,tk.END) for row in backend.database.search(self.name_text.get(),self.color_text.get()): self.listBox.insert(tk.END, row)
backend.py class database:
def search(name="",color=""): try: connect = sqlite3.connect("color.db") cur = connect.cursor() sql = "SELECT * FROM color WHERE name=? OR color=?" values = (self, name_text.get(), color_text.get()) cur.execute(sql, values) rows = cur.fetchall() name_text.set(rows[1]) color_text.set(rows[2]) entry_name.configure('disabled') combobox2.configure('disabled') connect.close() except: messagebox.showinfo('nothing found!')
I also tried to put a self in in an other version of backend.py. This gives the same error.
def search(self, name="",color=""): try: self.connect = sqlite3.connect("color.db") self.cur = self.connect.cursor() self.sql = "SELECT * FROM color WHERE name=? OR color=?" self.values = (self, name_text.get(), color_text.get()) self.cur.execute(sql, values) self.rows = self.cur.fetchall() self.name_text.set(rows[1]) self.color_text.set(rows[2]) self.entry_name.configure('disabled') self.combobox2.configure('disabled') self.connect.close() except: messagebox.showinfo('nothing!')
Please help solve the error:
for row in backend.database.search(self.name_text.get(),self.color_text.get()): TypeError: 'NoneType' object is not iterable
There are few issues on the backend.database.search()
function:
name_text
and color_text
are undefinedname
and color
should be used in values
insteadBelow is a modified search()
function:
def search(name="", color=""):
rows = () # assume no result in case of exception
try:
connect = sqlite3.connect("color.db")
cur = connect.cursor()
sql = "SELECT * FROM color WHERE name=? OR color=?"
values = (name, color) # use arguments name and color instead
cur.execute(sql, values)
rows = cur.fetchall()
connect.close()
except Exception as e:
print(e) # better to see what is wrong
messagebox.showinfo('nothing found!')
return rows # return result