Skip to content

Instantly share code, notes, and snippets.

@khunreus
Created April 12, 2019 15:48
Show Gist options
  • Save khunreus/65600ff0a04fa7c18716958c488ec480 to your computer and use it in GitHub Desktop.
Save khunreus/65600ff0a04fa7c18716958c488ec480 to your computer and use it in GitHub Desktop.
"""
python3.6
writes a csv to MySQL database
"""
mydb = mysql.connector.connect(host = 'localhost',
user = 'root',
passwd = '****',
db = '****')
cursor = mydb.cursor()
csv_file = 'path/to/file.csv'
with open(csv_file, 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader) #skipping the header
for row in csv_reader:
cursor.execute("INSERT INTO table(col1, col2, col3) VALUES (%s, %s, %s)", row)
mydb.commit()
cursor.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment