Update function sqldf R Language

穿精又带淫゛_ 提交于 2019-12-22 09:16:11

问题


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.


回答1:


As Joran mentioned in a comment this question is an sqldf FAQ. In fact its sqldf FAQ #8.

As discussed there the problem is that you asked to update the table but never asked it to return the table. Also $'contract' should be '$contract' since you want to substitute in contract and then surround that substitution with single quotes.

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

Now that we have some data try this:

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

The result is:

   V1
1 %x%
2   b
3   c

This would work too:

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


来源:https://stackoverflow.com/questions/20130417/update-function-sqldf-r-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!