RpostgreSQL 如何在数据框中转换 bytea 列 RDS 格式

问题描述

我需要从 postgeSQL 数据库的 BYTEA 字段中提取数据。在BYTEA字段中,数据以RDS格式存储(它是一个dataframe对应的saveRDS命令创建的.rds文件的内容)。由于此链接中给出的代码,使用 RPostgreSQL 包,我能够检索字符对象中的 bytea 列 https://github.com/codeinthehole/rpostgresql/blob/master/RPostgreSQL/tests/bytea.R

但是我怎样才能在相应的数据帧中转换我的角色对象(以 RDS 格式)? 我尝试使用 readRDS,但它似乎只适用于文件。

谢谢

## (supposing the connection to the database)
## I do the select statement to retrieve the bytea column
##
rows <- dbGetQuery(con,"SELECT image_bytea from tabdata")
str(rows)
# 'data.frame': 1 obs. of  1 variable:
#   $ image_bytea: chr "\\x1f8b08000000000000065| __truncated__
str(rows[1,1])
# chr "\\x1f8b08000000000000065c9d655456585"| __truncated__
test <- postgresqlUnescapeBytea(rows[1,1])
conraw <- rawConnection(test,open="r")
mydf <- readRDS(file=conraw)
# Error in readRDS(file = conraw) : format d'entrée inconnu

当我在 cmd MSwindows 中使用 psql 时,它工作正常:

psql -At -p 5432 -h localhost -d cabri -U postgres -W -c "select lo_from_bytea(0,image_bytea) from tabdata'
# 31385
psql -At -p 5432 -h localhost -d cabri -U postgres -W -c "\lo_export 31385 'c:/upload-postgre/test-out.rds'"

在 R 中:

mydf <- readRDS(file = "c:/upload-postgre/test-out.rds")
class(mydf)
##   [1] "data.frame"
head(mydf)

##     tps      pc1     pc1f pc1f_partiel
## 1 -0.4000000 154.2554 70.62402     157.4627  93.89868 
## 2 -0.3999995 154.2127 70.94351     157.4627 153.9946 
## etc... 

劳伦特

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...