【发布时间】:2018-06-18 06:15:02
【问题描述】:
在我的计算中,我得到了一些存储在向量中的结果。因为这些计算是重复执行的,所以我会在我的数据库中存储一些向量。 在我的数据库表 data_storage 中,每一行都应该存储一个结果向量。
直到现在我发现我的表中需要一个 BLOB 变量,并且向量必须按照Storing R Objects in a relational database 中的说明进行序列化。 在这个提到的来源上,David Josipovic 的答案似乎非常适合我,但我无法正确编码。查看我的代码中的数据输入...
EDIT_1:使用dbGetQuery、dbExecute() 或dbBind() 时会出现错误消息。序列化错误(res_1.v):缺少连接参数(没有默认值)。
对我来说,重要的是要知道如何在数据库中获取结果向量以及如何将它们取出。 所以我希望你能帮助我。
提前非常感谢!
我的代码:
# 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)
【问题讨论】:
-
如果您尝试插入,您可能需要
dbExecute()而不是dbGetQuery()。 -
@Marius 和
dbExecute()也会发生错误。连接丢失并在括号中“没有默认值” -
@tueftla 如果你想使用语句,那么我认为你需要
dbSendStatement。dbExecute用于不带参数的直接 DML 命令。
标签: r sqlite serialization rsqlite