0

I want to add real value calculated earlier on a postgresql database

Imp1=5/4
req=paste("INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'temperature',Imp1)")
resultat=dbGetQuery(con,req)

error

Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not Retrieve the result : ERREUR:  la colonne « imp1 » n'existe pas
LINE 1: ...m_cluster,type,indicateur_imp) VALUES (1,'temperature',Imp1)
                                                                  ^
)
Warning message:
In postgresqlQuickSQL(conn, statement, ...) :
  Could not create executeINSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'temperature',Imp1)
5
  • 1
    Typically, people will build queries in R using either paste or sprintf. Commented Jan 29, 2015 at 17:33
  • yes I used "paste" the error is on the value of "Imp1" how to add a real number on queries? Commented Jan 29, 2015 at 17:36
  • 1
    You "used" paste, but you didn't use paste. Compare paste("some text Imp") and paste("some text",Imp). Commented Jan 29, 2015 at 17:38
  • I was unable to find the right syntax I am new to R Commented Jan 29, 2015 at 17:55
  • req=paste("INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'temperature' ))",Imp1,sep="") resultat=dbGetQuery(con,req) Commented Jan 29, 2015 at 17:56

1 Answer 1

1

I think should give you what you're looking for:

Imp1 <- 32.1
req  <- paste("INSERT INTO important (num_cluster,type,indicateur_imp) 
VALUES (1,'temperature',",Imp1, ")",sep="")

That will output:

[1] "INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'temperature',32.1)"

paste() will take any variable and return it's value as a string, and you chain items together with commas. You can end the statement with a sep= parameter. To be the most literal (which I like doing for queries) I just use sep = "" and type the whole thing literally. So If I had multiple values I wanted to insert I would do something like this:

var1 <- round(rnorm(5))
var2 <- c("'temp'","'pressure'","'precip'","'volume'","'mass'")
for(i in 1:length(var1)){
req <- paste("INSERT INTO important (num_cluster,type,indicateur_imp) VALUES   
(1,",var2[i],",",var1[i],")",sep="")
# not run, but do to send your query 
# dbSendQuery(conn,req)
print(req)
}

That will send the following queries:

[1] "INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'temp',0.09)"

[1] "INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'pressure',0.04)"

[1] "INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'precip',-1.2)"

[1] "INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'volume',0.78)"

[1] "INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'mass',1.15)"

Sign up to request clarification or add additional context in comments.

1 Comment

Note that when you send strings to a query, they are stored in the vector with double and single quotes e.g. "'temp'" not "temp" (assuming you want to send a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.