In my calculations I get some results which are stored in a vector. And because these calculations are executed repeatedly I will have some vectors to be stored in my database. In my database table data_storage in each row a result vector should be stored.
Until now I found out that I need a BLOB variable in my table and the vectors must be serialized as mentioned in Storing R Objects in a relational database. On this mentioned source the answer from David Josipovic seems to be perfectly fitting to me but I can't code it right. See the data input in my code...
EDIT_1: when using dbGetQuery
, dbExecute()
or dbBind()
an error message occures. Error in serialize(res_1.v): The connections argument is missing (without default).
For me it is important to know how to get the result vectors in the database and also how to get them out. So I hope, that you can help me.
Many thanks in advance!
My Code:
# needed packages for this script
# install.packages("sqldf") # install this package if necessary
library(sqldf)
# connection to the database
db=dbConnect(SQLite(), ":memory:")
# creation of the database table
dbSendQuery(conn = db,
"CREATE TABLE IF NOT EXISTS data_storage
(ID INTEGER,
data BLOB,
PRIMARY KEY (ID))")
# calculations
# ....
# the first result vector
res_1.v=seq(from=1,to=10,by=1)
# the second result vector
res_2.v=seq(from=5,to=7,by=0.1)
# filling the data_storage table with the result vectors (in two rows)
### here an error occures
dbGetQuery(db, 'INSERT INTO data_storage VALUES (1,:blob)', params = list(blob = list(serialize(res_1.v)))) # the first row with res_1.v
dbGetQuery(db, 'INSERT INTO data_storage VALUES (2,:blob)', params = list(blob = list(serialize(res_2.v)))) # the second row with res_2.v
# getting the serialized result vector out of the database
# and converting them again to the result vector res_1.v respectively res_2.v
#######################################
### the still missing code sequence ###
#######################################
# close the connection
dbDisconnect(db)