Apr-15-2019, 12:45 PM
(Apr-15-2019, 12:51 AM)ichabod801 Wrote: When you say it's not reading anything, what exactly do you mean? Is it going to the error in the else clause no matter what, or is it just not outputting anything? Also, where did you move the sql code to when you added the if/else? And please post a few lines of your name file we can test with.
Sorry - I didn't copy the entire code. It's not going to the error clause, it's just not putting anything into the database, and if I ask it to print what's in the file, it doesn't print anything. Here's the code:
file = askopenfilename(parent = root) if file.endswith('.csv'): with open(file, 'r') as names: titles = csv.reader(names) titles = list(titles) titles = titles[0] if titles == ['Name', 'Surname']: toDB = [] reading = csv.DictReader(names) for row in reading: if (row['Name']).isalpha() and (row['Surname']).isalpha(): toDB.append((i['Name'], i['Surname'])) else: root = tk.Tk() error = tk.Label(root, text = "Error: invalid titles. Must be 'Name' and 'Surname'.") error.pack() root.after(5000, lambda: root.destroy()) root.mainloop() root = tk.Tk() root.geometry("0x0") dbChoice(root) else: root.destroy() root = tk.Tk() error = tk.Label(root, text = "Invalid file: can only be a .csv file") error.pack() root.after(5000, lambda: root.destroy()) root.mainloop() root = tk.Tk() root.geometry("0x0") dbChoice(root) accounts = sqlite3.connect("accounts.db") c = accounts.cursor() c.executemany('''INSERT INTO Students (FirstName, Surname) VALUES (?, ?)''', toDB)