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?
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
sqlite3package passes it around efficiently. If you’re still concerned, here’s the other way (again, you’ll need to replace...appropriately):