Created
April 12, 2019 15:48
-
-
Save khunreus/65600ff0a04fa7c18716958c488ec480 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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