[英]Write values from R to a PostgreSQL table based on Row IDs
I have a PostgreSQL table Scores
on a local server that looks like this:我在本地服务器上有一个 PostgreSQL 表
Scores
,如下所示:
ID Score_X Score_Y
1 NA NA
2 NA NA
3 NA NA
4 NA NA
I do a series of calculations in R that produces a dataframe Calc_Scores
that looks like this:我在 R 中进行了一系列计算,生成的 dataframe
Calc_Scores
如下所示:
ID Score_X Score_Y
1 0.53 0.81
4 0.75 0.95
I would like to write the scores that correspond with each ID from R to the PostgreSQL table such that the final PostgreSQL table should look like this:我想将与 R 中的每个 ID 对应的分数写入 PostgreSQL 表,这样最终的 PostgreSQL 表应如下所示:
ID Score_X Score_Y
1 0.53 0.81
2 NA NA
3 NA NA
4 0.75 0.95
I have a connection to the PostgreSQL table called connection
which I setup using the function dbConnect()
.我有一个到 PostgreSQL 表的连接,称为
connection
,我使用 function dbConnect()
设置。 The actual tables are quite big.实际的桌子很大。 What line/code in R could I use to write these scores to the PostgreSQL table?
我可以使用 R 中的哪些行/代码将这些分数写入 PostgreSQL 表? I have been looking for a similar question but couldn't find anything.
我一直在寻找类似的问题,但找不到任何东西。 I have tried
我努力了
dbWriteTable(connection, "Scores", value = Calc_Scores, overwrite=T, append = F, row.names = F)
However, the entire table gets overwritten.但是,整个表都会被覆盖。 I want only the scores to be updated.
我只想更新分数。
Thank you.谢谢你。
Creating a temporary table could be an option:创建临时表可能是一种选择:
# Create temporary table
dbWriteTable(connection, "ScoresTmp", value = Calc_Scores, overwrite=T, append = F, row.names = F)
# Update main table
dbExecute(connection,"
UPDATE Scores
SET Score_X = ScoresTmp.Score_X,
Score_Y = ScoresTmp.Score_Y
FROM ScoresTmp
WHERE Scores.ID = ScoresTmp.ID
")
# Clean up
dbExecute(connection,"DROP TABLE ScoresTmp")
Note that you should be able to create a real temporary table using the temporary=TRUE
option: according to @Sirius comment below, it should work on a PostGreSQL
database.请注意,您应该能够使用
temporary=TRUE
选项创建一个真正的临时表:根据下面的@Sirius 评论,它应该适用于PostGreSQL
数据库。
For users of an SQLServer
database, this option doesn't work, but they can use the #
prefix to create a temporary table.对于
SQLServer
数据库的用户,此选项不起作用,但他们可以使用#
前缀创建临时表。
In the example above, this would be:在上面的示例中,这将是:
dbWriteTable(connection, "#ScoresTmp", value = Calc_Scores, overwrite=T, append = F, row.names = F)
One way of doing this relies on the SQL 'update' and in essence you do这样做的一种方法依赖于 SQL '更新',本质上你做
- open a connection to your database
- loop over your changeset and for each row
- form the update statement, i.e. for example via
cmd <- paste('update table set x=', Score_x, ', y=',
Score_y, ' where id=', id)
- submit the cmd via eg `dbSendQuery`
- close the connection
There are examples in RPostgreSQL
. RPostgreSQL
中有示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.