I am looking to insert a python dataframe into MSSQL table using the below code:
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};\
Server=esql.ecs.local;\
Database=drgt;\
Trusted_Connection=yes;')
cursor = conn.cursor() #Create cursor
for row_count in range(0, t6.shape[0]):
chunk = t6.iloc[row_count:row_count+1,:].values.tolist()
tuple_of_tuples = tuple(tuple(x) for x in chunk)
cursor.executemany("insert into DWWorking.dbo.api_Nic_Data"+"([[a],[b],[c],[d],[e],[f],\
[g],[h],[i],[j],[k],[l],[m],[n],\
[o],[p],[q],[r],[s],[t],[u],[v],\
[w],[x],[y],[z],[ab],[cd],\
[ef],[gh],[ij],[kl]])\
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
tuple_of_tuples)
The cursor is opened and I checked that the table exists and has the required columns. I still receive the below error while executing:
ProgrammingError: ('The SQL contains 0 parameter markers, but 33 parameters were supplied', 'HY000')
How can I resolve?
df.to_sql()using asqlalchemyengine? Surely more simple and likely more efficient. pandas docs here and sqlalchemy connstr docs here.'DWWorking.dbo.api_Nic_Data'? I’d keep trying with this, you’re on the right track. TBH I’m not sure how much of the tablename pandas is looking for; (I avoid MSSQL at all cost). But again, you’re on the right track.df.to_sql().