Skip to content

Instantly share code, notes, and snippets.

@Marceloromeugoncalves
Created March 30, 2021 22:57
Show Gist options
  • Save Marceloromeugoncalves/3f1b3b263ea514f429920bf1cd62f0f4 to your computer and use it in GitHub Desktop.
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.
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