I have written code to show data from a sql query in tkinter. From a search function I want to print out the first item in the tuple on one line, then the rest of the results on another. I know that every tuple in the tuple has four data items (date, name , start time, end time), but I cant figure out how to get it to work properly.
def searched():
Main_Screen.configure(state = 'normal')
Main_Screen.delete('1.0', END)
searched_for = search.get()
timeslot = cursor.execute('''SELECT * FROM dates WHERE Name = (?)''',(searched_for,))
list1 = list(cursor.fetchall())
print(list1)
cursor.execute('''SELECT COUNT(*) FROM dates WHERE Name = (?) ''',(searched_for,))
result = cursor.fetchone()[0]
print(result)
n = 1
m = 1
for i in range(0, result - 1):
my_variable = list1[m-1][n-1]
Main_Screen.insert(tkinter.END,my_variable)
Main_Screen.insert(tkinter.END, "\n")
for i in range(0,3):
my_var2 = list1[m-1][2-1]
Main_Screen.insert(tkinter.END,my_var2)
my_var3 = list1[m-1][3-1]
Main_Screen.insert(tkinter.END,my_var3)
my_var4 = list1[m-1][4-1]
Main_Screen.insert(tkinter.END,my_var3)
Main_Screen.insert(tkinter.END, "\n")
m = m + 1
For example I search for everything with the name 'test'. The list I receive is:
[('2017-07-14', 'test', '11:00:00', '13:00:00'), ('2017-07-21', 'test', '11:00:00', '13:00:00'), ('2017-07-28', 'test', '11:00:00', '13:00:00'), ('2017-08-04', 'test', '11:00:00', '13:00:00'), ('2017-08-11', 'test', '11:00:00', '13:00:00'), ('2017-03-10', 'test', '12:00:00', '14:00:00')]
6(how many are in the main tuple)
but when it prints to the gui:
2017-07-14
test11:00:0011:00:00
test11:00:0011:00:00
test11:00:0011:00:00
2017-08-04
test11:00:0011:00:00
test11:00:0011:00:00
test12:00:0012:00:00
What I want:
Date of timeslot
Name of Timeslot
Start Time
End Time
Also for some reason I get this error:
File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 124, in searched
my_variable = list1[m-1][n-1]
IndexError: list index out of range
EDIT=
def searched():
Main_Screen.configure(state = 'normal')
Main_Screen.delete('1.0', END)
searched_for = search.get()
timeslot = cursor.execute('''SELECT * FROM dates WHERE Name = (?)''',(searched_for,))
list1 = list(cursor.fetchall())
print(list1)
cursor.execute('''SELECT COUNT(*) FROM dates WHERE Name = (?) ''',(searched_for,))
result = cursor.fetchone()[0]
print(result)
for tuple_vars in list1:
for var0, var1, var2 ,var3 in tuple_vars:
header = '{}\n'.format(var0)
Main_Screen.insert(tkinter.END, header) # ADD SOME FORMATTING
output = '{}\n{}\n{}\n{}\n'.format(var1, var2, var3)
Main_Screen.insert(tkinter.END, output)
for i in range(0, result - 1):
Main_Screen.insert(tkinter.END, "\n")
and get new error:
File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 122, in searched
for var0, var1, var2 ,var3 in tuple_vars:
ValueError: too many values to unpack (expected 4)