Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update function sqldf R Language

Tags:

r

sqldf

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.

like image 861
Tony Avatar asked Feb 14 '23 14:02

Tony


1 Answers

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
like image 89
G. Grothendieck Avatar answered Feb 28 '23 07:02

G. Grothendieck