简体   繁体   English

将数据帧写入sql表

[英]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:

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. 113行,而LNAME_EMP是有效名称,并且每一行都包含数据。

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! 谢谢!

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"]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM