我想在postgresql数据库中添加之前计算的实际值
Imp1=5/4
req=paste("INSERT INTO important (num_cluster,type,indicateur_imp) VALUES (1,'temperature',Imp1)")
resultat=dbGetQuery(con,req)
错误
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)
答案 0 :(得分:1)
我认为应该给你你想要的东西:
Imp1 <- 32.1
req <- paste("INSERT INTO important (num_cluster,type,indicateur_imp)
VALUES (1,'temperature',",Imp1, ")",sep="")
那将输出:
[1]&#34; INSERT INTO important(num_cluster,type,indicationur_imp)VALUES(1,&#39; temperature&#39;,32.1)&#34;
paste()
将获取任何变量并将其值作为字符串返回,并将项目与逗号链接在一起。您可以使用sep=
参数结束语句。要成为最文字的(我喜欢为查询做)我只使用sep = ""
并按字面输入整个事物。所以如果我想要插入多个值,我会做这样的事情:
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)
}
这将发送以下查询:
[1]&#34; INSERT INTO important(num_cluster,type,indicationur_imp)VALUES(1,&#39; temp&#39;,0.09)&#34;
[1]&#34; INSERT INTO重要(num_cluster,type,indicationur_imp)VALUES(1,&#39;压力&#39;,0.04)&#34;
[1]&#34; INSERT INTO important(num_cluster,type,indicationur_imp)VALUES(1,&#39; degra&#39;, - 1.2)&#34;
[1]&#34; INSERT INTO important(num_cluster,type,indicationur_imp)VALUES(1,&#39; volume&#39;,0.78)&#34;
[1]&#34; INSERT INTO important(num_cluster,type,indicationur_imp)VALUES(1,&#39; mass&#39;,1.15)&#34;