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 开发者_如何学C-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?
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:
- 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
- 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
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论