python insert tuple to sql table with executemany pyodbc

38 views Asked by At

probably I have have a siple error but I have 2 days trying to resolve it.

This is my code:

#insert table #import pyodbc #instalar en el prompt de anaconda con pip install pyodbc

#server = 'FERA10\MSSQLSERVER01' bd = 'DB_Python3'

try:

#conexion = pyodbc.connect(driver='{SQL server}', host = server, database = bd) 
conexion = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}',host = server,Trusted_Connection='yes',autocommit=True)
print('Conexión exitosa')

except:

print('La conexión no fué exitosa')

#Creamos un cursor para almacenar la información en memoria cursor = conexion.cursor()

cursor.executemany("INSERT INTO MaratonNY_Python(Corredor INTEGER, place INTEGER, gender VARCHAR(25), age INTEGER, home VARCHAR(255), tiempo FLOAT, PRIMARY KEY (Corredor) VALUES(?,?,?,?,?,?)", tuplas_lista)

#Guardamos los cambios hechos a la Base de Datos cursor.commit()

#Nos aseguramos de cerrar la conexión conexion.close()

But I get this error:

Conexión exitosa

ProgrammingError Traceback (most recent call last) Cell In[68], line 20 17 #Creamos un cursor para almacenar la información en memoria 18 cursor = conexion.cursor() ---> 20 cursor.executemany("INSERT INTO MaratonNY_Python(Corredor INTEGER, place INTEGER, gender VARCHAR(25), age INTEGER, home VARCHAR(255), tiempo FLOAT, PRIMARY KEY (Corredor) VALUES(?,?,?,?,?,?)", tuplas_lista) 23 #Guardamos los cambios hechos a la Base de Datos 24 cursor.commit()

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near 'INTEGER'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")

Do you know what is my error?

0

There are 0 answers