简体   繁体   English

R PostgreSQL-数据库表中的数据未更新

[英]R PostgreSQL - data is not getting updated in the table of the database

Hi I am trying to update a postgresql table using RpostgreSQL package, the commands in R are executed successfully but the new data is not getting reflected in the database. 嗨,我正在尝试使用RpostgreSQL包更新postgresql表,R中的命令已成功执行,但是新数据未反映在数据库中。 Below are the commands i have executed in R 以下是我在R中执行的命令

for(i in new_data$FIPS) {
  drv <- dbDriver("PostgreSQL")
  con <- dbConnect(drv, dbname="ip_platform", host="******", port="5432", user="data_loader", password="******")
  txt <- paste("UPDATE adminvectors.county SET attributes= hstore('usco@TP-TotPop@2010'::TEXT,",new_data$TP.TotPop[new_data$FIPS == i],"::TEXT) where geoid ='",i,"'")
  dbGetQuery(con, txt)
  dbCommit(con)
  dbDisconnect(con)
}

Can anyone let me know if I have done something wrong? 有人可以让我知道我做错了什么吗? Any help is highly appreciated 任何帮助都受到高度赞赏

You are calling dbGetQuery instead of dbSendQuery and also disconnecting from the database in your for loop. 您正在调用dbGetQuery而不是dbSendQuery,并且还在for循环中从数据库断开连接。 You also are creating a new connection object for every loop iteration, which is not necessary. 您还将为每次循环迭代创建一个新的连接对象,这不是必需的。 Try this: 尝试这个:

drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname="ip_platform", host="******", port="5432", user="data_loader", password="******")

for(i in new_data$FIPS) {
  txt <- paste("UPDATE adminvectors.county SET attributes= hstore('usco@TP-TotPop@2010'::TEXT,",new_data$TP.TotPop[new_data$FIPS == i],"::TEXT) where geoid ='",i,"'")
  dbSendQuery(con, txt)
}

dbDisconnect(con)

You shouldn't call dbCommit(con) explicitly. 您不应显式调用dbCommit(con) The enclosing transaction will always be commited when dbSendQuery returns, exactly as when you do an UPDATE with pure SQL. dbSendQuery返回时,将始终提交封闭的事务,这与使用纯SQL进行UPDATE时完全相同。 You don't call COMMIT unless you have created a new transaction with BEGIN TRANSACTION . 除非您使用BEGIN TRANSACTION创建了一个新事务,否则您不会调用COMMIT

The warning "there is no transaction in progress" is PostgreSQL's way of telling you that you have issued a COMMIT statement without having issued a BEGIN TRANSACTION statement which is exactly what you are doing in your function. 警告“没有正在进行的事务”是PostgreSQL告诉您已发出COMMIT语句而未发出BEGIN TRANSACTION语句的方式,而这正是您在函数中所做的。

Simplify, simplify, simplify -- the RPostgreSQL has had unit tests for these types of operations since the very beginning (in 2008 no less) and this works (unless you have database setup issues). 简化,简化,简化-RPostgreSQL从一开始就对这些类型的操作进行了单元测试(至少在2008年),并且这种方法有效(除非存在数据库设置问题)。

See eg here in the GitHub repo for all the tests. 有关所有测试,请参见例如GitHub存储库中的此处

Thanks for all your inputs. 感谢您的所有投入。 The issue is with the paste() function which I used in the for loop. 问题出在我在for循环中使用的paste()函数。 The paste() function has replaced the comma with a space in the query as a result the where condition is failing. paste()函数已在查询中用空格替换逗号,结果where条件失败。 I have added a sep="" attribute in the paste() and the query is now properly sent to the database and the rows are getting updated as expected. 我在paste()中添加了sep =“”属性,查询现在已正确发送到数据库,并且行按预期进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM