【发布时间】:2015-01-14 14:17:38
【问题描述】:
我想从 R 的 sqlite 数据库中读取二进制字段。
我通过一种间接方法成功了,包括首先通过调用 sqlite3 的 shell 命令将二进制字段导出到临时文件中,然后使用 readbin() 命令读取此二进制文件。详细代码如下:
library(RSQLite)
fileConn<-file("script.sql")
writeLines(c('.load fileio.dll','SELECT writefile("tmp.bin",Curve) FROM Table1 WHERE Id=41;','.exit'), fileConn)
close(fileConn)
shell('sqlite3.exe database.sqlite < script.sql')
to.read<-file(description="tmp.bin","rb")
tmp<-readBin(to.read,"integer",n=10000,size=2)
plot(tmp)
close(to.read)
我想用 R 直接读取二进制字段,而不是通过 sqlite3 创建临时文件。
根据建议(非常感谢!),我尝试了以下方法:
con <- dbConnect(SQLite(),dbname="database.sqlite")
tmp<-dbGetQuery(con,"SELECT quote(Curve) FROM Table1 WHERE Id=41")
tmp
返回:
quote(Curve)
1 '\023'
看起来不太好,因为我应该得到一个包含 870 个整数元素的向量,以 :
开头head(good)
[1] 19 19 0 19 19 19
我的 sql 语句出了什么问题?如何使用 R 直接读取二进制字段?
非常感谢您
【问题讨论】:
-
之后你可以
base::unlink这个文件吗? -
dbGetQuery(con,'SELECT有什么不适合你的?