Created
March 30, 2021 22:57
-
-
Save Marceloromeugoncalves/3f1b3b263ea514f429920bf1cd62f0f4 to your computer and use it in GitHub Desktop.
Inerir dados de um arquivo CSV em um banco de dados MySQL com Python.
This file contains 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
import csv | |
import MySQLdb | |
mydb = MySQLdb.connect(host='127.0.0.1', user='root', password='', database='all_db') | |
with open('cars.csv') as csv_file: | |
csvfile = csv.reader(csv_file, delimiter=',') | |
all_value = [] | |
for row in csvfile: | |
value = (row[0], row[1], row[2]) | |
all_value.append(value) | |
query = 'INSERT INTO `tbl_cars` (`name`, `company`, `launch_year`) values (%s, %s, %s)' | |
mycursor = mydb.cursor() | |
mysurcor.executemany(query, all_value) | |
mydb.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment