使用 Tcl/Tk 打开存储在 Sqlite 中的 Blob 数据作为文件
我正在使用以下 tcl 代码将文件从桌面存储到 Sqlite 数据库中作为 blob 数据($fileText
是文本文件的路径):
sqlite3 db Docs.db
set fileID [open $fileText RDONLY]
fconfigure $fileID -translation binary
set content [read $fileID]
close $fileID
db eval {insert into Document (Doc) VALUES ($content)}
db close
我找到了许多有关如何打开blob 数据读取和写入它,但我找不到任何有关将 blob 数据作为文件打开的资源。例如,如果 $fileText 是 pdf,我如何从 Sqlite 将其打开为 pdf?
I am using the following tcl code to store a file from my deskstop into a Sqlite database as blob data ($fileText
is a path to a text file):
sqlite3 db Docs.db
set fileID [open $fileText RDONLY]
fconfigure $fileID -translation binary
set content [read $fileID]
close $fileID
db eval {insert into Document (Doc) VALUES ($content)}
db close
I have found many resources on how to open the blob data to read and write to it, but I cannot find any resources on opening the blob data as a file. For example, if $fileText was a pdf, how would I open it, from Sqlite, as a pdf?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说“以 PDF 形式打开”时,我假设您的意思是您希望某些外部程序将数据作为文件查看?唯一的方法是:
还有将其全部作为网络服务器呈现,但这实际上是第二个与浏览器混合在一起的;数据仍然被复制。
另一方面,如果您想要做的只是将数据作为可以从 Tcl 读取或写入的流,则 sqlite3 包可以满足您的需要:
它返回一个标准通道句柄(尽管不是由操作系统备份的)句柄,因此在使用
exec
进行重定向时要小心)。[编辑]:以下是如何获取数据(当然,用子句替换
...
以获得正确的行):不要担心 BLOB 的大小; Tcl
sqlite3
包有效地传递它。如果您仍然担心,可以使用另一种方法(同样,您需要适当地替换...
):When you say “open as a PDF”, I assume that you mean that you want some external program to see the data as a file? The only ways to do that are either:
There's also presenting it all as a webserver, but that's really the second with a browser in the mix; the data is still copied.
On the other hand, if all you want to do is have the data as a stream that you can read or write from Tcl, the sqlite3 package has what you need:
Which returns a standard channel handle (though not one backed up by an OS handle, so be careful if using as a redirection with
exec
).[EDIT]: Here's how to getthe data out (replace
...
with a clause to get the right row, of course):Don't worry about the size of the BLOB; the Tcl
sqlite3
package passes it around efficiently. If you're still concerned, here's the other way (again, you'll need to replace...
appropriately):