Ejemplo 1: Python mysql insertar csv
# Insert DataFrame to Table
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()
Ejemplo 2: importar Excel csv a mysql con python
import pandas as pd
import pyodbc
# Import CSV
data = pd.read_csv (r'C:UsersRonDesktopTestPeople.csv')
df = pd.DataFrame(data, columns= ['Name','Country','Age'])
# Connect to SQL Server
conn = pyodbc.connect('Driver={SQL Server};'
'Server=RONSQLEXPRESS;'
'Database=TestDB;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
# Create Table
cursor.execute('CREATE TABLE people_info (Name nvarchar(50), Country nvarchar(50), Age int)')
# Insert DataFrame to Table
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()
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)