Skip to content Skip to sidebar Skip to footer

Inserting Data Into A Sql Server From An Excel File

First of all, sorry for my lack of knowledge regarding databases, this is my first time working with them. I am having some issues trying to get the data from an excel file and put

Solution 1:

Try this, you seem to have changed the library from pyodbc to mysql, it seems to expect %s instead of ?

import pymysql
import pandas as pd

connStr = pymysql.connect(host = 'xx.xxx.xx.xx', port = xxxx, user = 'xxxx', password = 'xxxxxxxxxxx') 

df = pd.read_csv('GenericProducts.csv')

cursor = connStr.cursor()

query = "INSERT INTO [Productos]([ItemID],[Nombre]) values (%s,%s)"

for index,row in df.iterrows():

    #cursor.execute("INSERT INTO dbo.Productos([ItemID],[Nombre]) values (%s,%s)", row['codigoEspecificoProducto'], row['nombreProducto'])
    codigoEspecificoProducto = row['codigoEspecificoProducto']
    nombreProducto = row['nombreProducto']

    values = (codigoEspecificoProducto,nombreProducto)

    cursor.execute(query,values)


connStr.commit()
cursor.close()
connStr.close() 

Post a Comment for "Inserting Data Into A Sql Server From An Excel File"