将矢量存储在关系数据库中

时间:2018-06-18 06:15:03

标签: r sqlite serialization rsqlite

在我的计算中,我得到了一些存储在矢量中的结果。并且因为这些计算重复执行,我将有一些向量存储在我的数据库中。 在每行的数据库表data_storage中,应存储结果向量。

到目前为止,我发现我的表中需要一个BLOB变量,并且必须按照Storing R Objects in a relational database中的提及序列化向量。 在这个提到的消息来源上,David Josipovic的答案似乎非常适合我,但我无法正确编码。查看我的代码中的数据输入......

EDIT_1:使用dbGetQuerydbExecute()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) 

2 个答案:

答案 0 :(得分:0)

也许你的语法有问题。这个RSQLite documentation page使用dbSendStatement来构建一个用于执行DML命令的预准备语句:

rs <- dbSendStatement(db, 'INSERT INTO data_storage VALUES (1, :blob)')
dbBind(rs, param = list(blob = serialize(res_1.v)))
dbGetRowsAffected(rs)
dbClearResult(rs)

这个答案假设API将正确地知道如何将BLOB绑定到语句中。

答案 1 :(得分:0)

感谢两位评论员Marius和Tim Biegeleisen以及一些反复试验的时间,我找到了一个解决方案......

代码的第一部分没有改变任何内容

# 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 you can/must use dbExecute() as suggested by Marius
### and in list(serialize(res_1.v,NULL)) the connection NULL is important
dbExecute(db, 'INSERT INTO data_storage VALUES (1,:blob)', params = list(blob = list(serialize(res_1.v,NULL))))  # the first row with res_1.v
dbExecute(db, 'INSERT INTO data_storage VALUES (2,:blob)', params = list(blob = list(serialize(res_2.v,NULL))))  # the second row with res_2.v

# reading out the content of table data_storage
dbReadTable(db,"data_storage")

# It's nearly the same - reading out the content of data_storage
dbGetQuery(db,'SELECT * FROM data_storage')
dbGetQuery(db,'SELECT * FROM data_storage')[,1]  # the content of the first column
dbGetQuery(db,'SELECT * FROM data_storage')[,2]  # the content of the second column - this is a BLOB

# get the result vector with its original entries
### and unlist() the BLOB entry
### and finally unserialize the unlisted BLOB entry
step_1=unlist(dbGetQuery(db,'SELECT * FROM data_storage')[1,2])  # here you must adjust the row index
step_2=unserialize(step_1)

# for control of equality
### step_2 is the converted res_1.v to BLOB and the reconverted
### so the outcome of identical() is TRUE
identical(res_1.v,step_2)

# close the connection
dbDisconnect(db)