Apr-14-2019, 05:50 PM
Hi,
I'm currently having an issue with my code. I have an option for a user to choose a .csv file with names and surnames of their students and this will be entered into the database. The data goes into the database fine as long as there are 'Name' and 'Surname' fields in the .csv file. This is an issue as if there is an invalid name in the .csv file (e.g. something with a number in), it will be entered into the database anyway, even if it shouldn't be allowed. I can't figure out how to validate the information before it goes into the database. Here is my code:
I'm currently having an issue with my code. I have an option for a user to choose a .csv file with names and surnames of their students and this will be entered into the database. The data goes into the database fine as long as there are 'Name' and 'Surname' fields in the .csv file. This is an issue as if there is an invalid name in the .csv file (e.g. something with a number in), it will be entered into the database anyway, even if it shouldn't be allowed. I can't figure out how to validate the information before it goes into the database. Here is my code:
file = askopenfilename(parent = root) if file.endswith('.csv'): with open(file, 'r') as names: reading = csv.DictReader(names) toDB = [(i['Name'], i['Surname']) for i in reading] accounts = sqlite3.connect("accounts.db") c = accounts.cursor() c.executemany('''INSERT INTO Students (FirstName, Surname) VALUES (?, ?)''', toDB)Can anyone help, please? <3