how to upload csv file to mysql in python code example
Example 1: python mysql insert csv
for row in df.itertuples():
cursor.execute('''
INSERT INTO TestDB.dbo.people_info (Name, Country, Age)
VALUES (?,?,?)
''',
row.Name,
row.Country,
row.Age
)
conn.commit()
Example 2: import excel csv into mysql with python
import pandas as pd
import pyodbc
data = pd.read_csv (r'C:\Users\Ron\Desktop\Test\People.csv')
df = pd.DataFrame(data, columns= ['Name','Country','Age'])
conn = pyodbc.connect('Driver={SQL Server};'
'Server=RON\SQLEXPRESS;'
'Database=TestDB;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('CREATE TABLE people_info (Name nvarchar(50), Country nvarchar(50), Age int)')
for row in df.itertuples():
cursor.execute('''
INSERT INTO TestDB.dbo.people_info (Name, Country, Age)
VALUES (?,?,?)
''',
row.Name,
row.Country,
row.Age
)
conn.commit()