-->

How to write and read binary data with RPostgresql

2019-09-13 13:26发布

问题:

I am trying to execute the code:

Connect to the server

library('RPostgreSQL', quietly = TRUE)
kHostName  <- '...'
kPort      <- '5432'
kDBName    <- '...'
kUser      <- '...'
kPassword  <- '...'

drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,
                 host      = kHostName,
                 port      = kPort,
                 dbname    = kDBName,
                 user      = kUser,
                 password  = kPassword)

The following part of the code is taken from https://groups.google.com/forum/#!topic/rpostgresql-dev/lPPmS8yeP9w and https://github.com/codeinthehole/rpostgresql/blob/master/RPostgreSQL/tests/bytea.R

dbGetQuery(con,"CREATE TABLE byteatable (name text NOT NULL, val bytea, 
PRIMARY KEY (name))") 
sample.object <- list("one","two"); 
ser <- serialize(sample.object,NULL,ascii=F); 
postgresqlEscapeBytea(con, ser) 
iq <- sprintf("INSERT INTO byteatable values('%s',E'%s');","name1", postgresqlEscapeBytea(con, ser)) 
dbGetQuery(con, iq) 
rows<-dbGetQuery(con, "SELECT * from byteatable") 
ser2<-postgresqlUnescapeBytea(rows[[2]])

It returns:

Error: could not find function "postgresqlEscapeBytea"

How can I make it work?

I use:

> version
               _                           
platform       x86_64-apple-darwin13.4.0   
arch           x86_64                      
os             darwin13.4.0                
system         x86_64, darwin13.4.0        
status                                     
major          3                           
minor          3.0                         
year           2016                        
month          05                          
day            03                          
svn rev        70573                       
language       R                           
version.string R version 3.3.0 (2016-05-03)
nickname       Supposedly Educational

回答1:

The function postgresqlUnescapeBytea is only available in the development version of the RpostgreSQL package as far as I'm aware, which can be downloaded here: https://github.com/codeinthehole/rpostgresql