Search code examples
pythonsqlpandaspypyodbc

writing data frames to a sql table


I'm reading from one database and outputting certain columns to another database. I use a data frame to store the data and then iterate through the frame to output the column i'm interested in.:

for i in range(0,len(myframe.index)):
    cursor_conn2.execute(SQL2_UPD_NEWEMP,myframe.loc[i,"LNAME_EMP"])

but I keep getting an error:

raise TypeError("Params must be in a list, tuple, or Row")

This is SQL2_UPD_NEWEMP:

SQL2_UPD_NEWEMP="INSERT INTO DBO.NEW_EMP_CAL(LNAME) VALUES(?)"      

there is data in the frame. 113 rows, and LNAME_EMP is a valid name, and every row contains data.

This should be quite simple, but I'm not seeing where the error is being made, and it makes me sad.

When I run this code I see all the data:

for i in range(0,len(myframe.index)):
    print(myframe.loc[i,"LNAME_EMP"])

Any help would be appreciated. Thanks!


Solution

  • Currently, you are passing a scalar as parameter. But as error clearly mentions, your parameter must be an iterable such as tuple:

    cursor_conn2.execute(SQL2_UPD_NEWEMP, (myframe.loc[i,"LNAME_EMP"],))
    

    Or a list:

    cursor_conn2.execute(SQL2_UPD_NEWEMP, [myframe.loc[i,"LNAME_EMP"]])