4

I have a problem with SQLdf. Although I am trying to update a table, it always gives NULL as an output. I red things about this problem but I cannot figure out how to solve it. My code is:

fn$sqldf("update cons set V1='%$numbernew%' where V1=$'contact'")

But after I check it to see if something has changed, all are the same as in the beginning. Any ideas would help.

4

1 回答 1

8

正如 Joran 在评论中提到的,这个问题是一个 sqldf FAQ。事实上它的sqldf FAQ #8

正如那里所讨论的,问题是您要求更新表但从未要求它返回表。也$'contract'应该是'$contract'因为您要替换,contract然后用单引号括住该替换。

# set up test data for reproduciblity
con <- data.frame(V1 = c("a", "b", "c"))
contract <- "a"
numbernew <- "x"

现在我们有一些数据试试这个:

sql1 <- fn$identity("update con set V1 ='%$numbernew%' where V1 = '$contract' ")
sql2 <- "select * from main.con"
sqldf(c(sql1, sql2))

结果是:

   V1
1 %x%
2   b
3   c

这也可以:

sqldf() # start a sequence of SQL statements

fn$sqldf("update con set V1 ='%$numbernew%' where V1 = '$contract' ")
ans <- sqldf("select * from main.con")

sqldf() # SQL statements finished
于 2013-11-21T23:35:29.770 回答