Take the 2-minute tour ×

I have a Tcl TK application that has a Sqlite back-end. I pretty much understand the syntax for inserting, manipulating, and reading string data; however, I do not understand how to store pictures or files into Sqlite with Tcl.

I do know I have to create a column that holds BLOB data in Sqlite. I just don't know what to do on the Tcl side of things. If anyone knows how to do this or has a good reference to suggest for me, I would really appreciate it.

Thank you,


share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

In my code, I basically open the file as a binary, load its content into a Tcl variable, and stuff that into the SQLite db. So, something like this...

# load the file's contents
set fileID [open $file RDONLY]
fconfigure $fileID -translation binary
set content [read $fileID}
close $fileID

# store the data in a blob field of the db
$db eval {INSERT OR REPLACE INTO files (content) VALUES ($content)}

Obviously, you'll want to season to taste, and you're table will probably contain additional columns...

share|improve this answer
Thanks Jeff - This looks promising. How is the variable $file set as a document? I understand that your code takes $file and changes it to a binary format so that it can be inserted into sqlite; however, how is the document (file, pdf, text, etc...) recognized as $file or set as $file? Thanks, DFM –  DFM Oct 27 '09 at 14:19
Damion, In my above code, "$file" is just a variable that holds the name of the file you want to store in the DB. So, it doesn't really matter what the file actually is. You only need to assign the name of the file to the "file" variable. Does that answer your question? –  Jeff Godfrey Nov 4 '09 at 22:08
add comment

The incrblob command looks like what you want: http://sqlite.org/tclsqlite.html#incrblob

The "incrblob" method


dbcmd  incrblob  ?-readonly??   ?DB?  TABLE  COLUMN  ROWID

The command returns a new TCL channel for reading or writing to the BLOB. The channel is opened using the underlying sqlite3_blob_open() C-langauge interface. Close the channel using the close command of TCL.

share|improve this answer
add comment

Your Answer


