使用 Tcl/Tk 打开存储在 Sqlite 中的 Blob 数据作为文件

发布于 2024-09-02 01:44:14 字数 417 浏览 4 评论 0原文

我正在使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-09-09 01:44:18

当您说“以 PDF 形式打开”时,我假设您的意思是您希望某些外部程序将数据作为文件查看?唯一的方法是:

  1. 在 Linux 上使用用户模式文件系统(或操作系统上的等效文件系统)进行一些巧妙的恶作剧,以便可以实际安装数据库,或者
  2. 使用以下命令将数据从数据库复制到临时文件中:正确的名称(提示:将其保留为该表中的单独列)。

还有将其全部作为网络服务器呈现,但这实际上是第二个与浏览器混合在一起的;数据仍然被复制。

另一方面,如果您想要做的只是将数据作为可以从 Tcl 读取或写入的流,则 sqlite3 包可以满足您的需要:

dbcmd incrblob ?-readonly? ?db? table column rowid

它返回一个标准通道句柄(尽管不是由操作系统备份的)句柄,因此在使用 exec 进行重定向时要小心)。


[编辑]:以下是如何获取数据(当然,用子句替换 ... 以获得正确的行):

# Open the DB
sqlite3 db Docs.db

# Open the file to write to
set fileID [open $fileText w]
fconfigure $fileID -translation binary

# Write the BLOB
db eval {SELECT Doc FROM Document WHERE ... LIMIT 1} row {
    puts -nonewline $fileID $row(Doc)
}

# We're done!
close $fileID
db close

不要担心 BLOB 的大小; Tcl sqlite3 包有效地传递它。如果您仍然担心,可以使用另一种方法(同样,您需要适当地替换 ...):

# Open the DB
sqlite3 db Docs.db

# Open the file to write to
set fileOut [open $fileText w]
fconfigure $fileOut -translation binary

# Get the BLOB as a (read-only) channel
set fdBlob [db incrblob -readonly Document Doc ...]
fconfigure $fdBlob -translation binary

# Do the copy 
fcopy $fileOut $fdBlob

# We're done!
close $fdBlob
close $fileOut
db close

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:

  1. Do some clever shenanigans with user-mode filesystems on Linux (or the equivalent on your OS) so that the database can be actually mounted, or
  2. Copy the data from the database into a temporary file with the right name (hint: keep that as a separate column in that table).

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:

dbcmd incrblob ?-readonly? ?db? table column rowid

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

# Open the DB
sqlite3 db Docs.db

# Open the file to write to
set fileID [open $fileText w]
fconfigure $fileID -translation binary

# Write the BLOB
db eval {SELECT Doc FROM Document WHERE ... LIMIT 1} row {
    puts -nonewline $fileID $row(Doc)
}

# We're done!
close $fileID
db close

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

# Open the DB
sqlite3 db Docs.db

# Open the file to write to
set fileOut [open $fileText w]
fconfigure $fileOut -translation binary

# Get the BLOB as a (read-only) channel
set fdBlob [db incrblob -readonly Document Doc ...]
fconfigure $fdBlob -translation binary

# Do the copy 
fcopy $fileOut $fdBlob

# We're done!
close $fdBlob
close $fileOut
db close
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文