I am currently working on a schoolproject, and im trying to import data from a CSV file to MySQL using python. This is my code so far:
import mysql.connector
import csv
mydb = mysql.connector.connect(host='127.0.0.1', user='root', password='abc123!', db='jd_university')
cursor = mydb.cursor()
with open('C:/Users/xxxxxx/Downloads/Students.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
cursor.execute('INSERT INTO Student (First_Name, Last_Name, DOB, Username, Password, Phone_nr,'
'Email, StreetName_nr, ZIP) '
'VALUES("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")',
row)
mydb.commit()
cursor.close()
When i run this, i get this error: "mysql.connector.errors.DataError: 1292 (22007): Incorrect date value: '%s' for column 'DOB' at row 1"
The date format used in the CSV file are yyyy-mm-dd
Any tips on this would help greatly!
print(row)for debug purpose.When finish remove it is ok.