Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在关系数据库中存储向量_R_Sqlite_Serialization_Rsqlite - Fatal编程技术网

R 在关系数据库中存储向量

R 在关系数据库中存储向量,r,sqlite,serialization,rsqlite,R,Sqlite,Serialization,Rsqlite,在我的计算中,我得到了一些存储在向量中的结果。因为这些计算是重复执行的,所以我会在数据库中存储一些向量。 在我的数据库表data_存储中,每行都应存储一个结果向量 直到现在,我才发现我的表中需要一个BLOB变量,并且必须按照中所述序列化向量。 在这个提到的消息来源上,大卫·约西波维奇的回答似乎非常适合我,但我无法正确编码。请参阅我的代码中的数据输入 编辑1:使用dbGetQuery,dbExecute()或dbBind()时出现错误消息。serialize(res_1.v)中出错:缺少conne

在我的计算中,我得到了一些存储在向量中的结果。因为这些计算是重复执行的,所以我会在数据库中存储一些向量。 在我的数据库表data_存储中,每行都应存储一个结果向量

直到现在,我才发现我的表中需要一个BLOB变量,并且必须按照中所述序列化向量。 在这个提到的消息来源上,大卫·约西波维奇的回答似乎非常适合我,但我无法正确编码。请参阅我的代码中的数据输入

编辑1:使用
dbGetQuery
dbExecute()
dbBind()
时出现错误消息。serialize(res_1.v)中出错:缺少connections参数(无默认值)

对我来说,知道如何在数据库中获取结果向量以及如何将它们取出是很重要的。 所以我希望,你能帮助我

非常感谢

我的代码:

# 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) 

也许您的语法有问题。这将使用
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)

rs多亏了两位评论员马吕斯和蒂姆·比格莱森,经过一段时间的反复试验,我找到了一个解决方案

在代码的第一部分中,没有任何更改

# 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)

如果要插入,可能需要使用
dbExecute()
而不是
dbGetQuery()
。@Marius with
dbExecute()
也会发生错误。连接丢失,括号中为“无默认值”@tueftla。如果要使用语句,则我认为需要
dbSendStatement
dbExecute
用于不带参数的直接DML命令。
dbBind()
也会导致错误。连接丢失,括号中为“无默认值”,那么可能是数据库连接对象有问题。在我的帖子中键入代码时,一切正常,直到我想存储结果向量为止。使用
dbListFields(db,“数据存储”)
将显示两列ID和数据…@tueftla,这并不意味着没有问题。在实际使用连接之前,您可能不会发现连接有问题。但是,我不是RSQLite方面的专家,只是想指出我认为使用DML准备语句的正确语法。