我有一个数据框data_frm
,其中包含以下列:
emp_id | emp_sal | emp_bonus | emp_desig_level
| | |
| | |
我想将此数据框中存在的所有记录插入数据库表tab1
。我执行了此查询但出现错误:
for(record in data_frm)
{
write_sql <- paste("Insert into tab1 (emp_id,emp_sal,emp_bonus,emp_desig_level) values (",data_frm[,"emp_id"],",",data_frm[,"emp_sal"],",",data_frm[,"emp_bonus"],",",data_frm[,"emp_desig_level"],")",sep="")
r <- dbSendQuery(r,write_sql)
}
我收到错误:
Error in data_frm[, "emp_id"] : incorrect number of dimensions
如何将数据框中的所有记录插入数据库?
注意:我想使用insert
语句插入数据框的所有记录。
答案 0 :(得分:1)
dbWriteTable(conn, "RESULTS", results2000, append = T) # to protect current values
dbWriteTable(conn, "RESULTS", results2000, append = F) # to overwrite values
来自sourceforge的RDBI homepage。希望有所帮助...
答案 1 :(得分:1)
在for循环中,您需要输入:
data_frm[record,"column_name"]
其他方面,你的循环试图插入整个列,而不仅仅是特定的记录。
for(record in data_frm)
{
write_sql <- paste("Insert into tab1 (emp_id,emp_sal,emp_bonus,emp_desig_level) values (",data_frm[record,"emp_id"],",",data_frm[record,"emp_sal"],",",data_frm[record,"emp_bonus"],",",data_frm[record,"emp_desig_level"],")",sep="")
r <- dbSendQuery(r,write_sql)
}
答案 2 :(得分:0)