[R] Saving R-objects to a database

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 25 21:28:57 CEST 2008


Presumably this about RMySQL, and by 'database' you mean a MySQL database, 
not e.g. a .rdb file?

R-sig-db would be a better list, but I think this is one of many aspects 
of the DBI package that that not been updated to match improvements in R.

On Thu, 25 Sep 2008, Christian Ruckert wrote:

> Someone solved the problem of saving R-objects to a database?
>
> These are the two varaints I've tried so far without success:
>
> 1)
> ser = rawToChar(serialize(obj, NULL, ascii=TRUE))
> dbSendQuery(link, paste("insert into table values(1, '",ser,"')",sep=''))

This will not work, because rawToChar can give embedded nuls.

> The field to save the object in the MySQL Database is of type text.
>
> unser = dbGetQuery(link,"select * from table where id=1")
> Warning message:
> In mysqlFetch(res, n, ...) :
>  RS-DBI driver warning: (internal error: row 0 field 1 truncated)
>
>
> 2)
> ser = serialize(obj, NULL)
> dbSendQuery(link, paste("insert into table values(1, '",ser,"')",sep=''))
>
> The field to save the object in the MySQL Database is of type blob.
>
> result = dbGetQuery(link,"select * from table where id=1")
>
> unser = unserialize(result[1,"object_column"], NULL)

DBI needs to convert blobs to raw not character.

> Error in unserialize(result[1, "object_column"], NULL) : unknown input format
> In addition: Warning message:
> In unserialize(result[1, "object_column"], NULL) :
>  unserialize()from a character string is deprecated and will be withdrawn in 
> R 2.8.0
>
>
> Any suggestion is welcome.
>
> Best,
> Christian

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list