tags:

views:

301

answers:

3

Hello:

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,

Damion

A: 

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

The "incrblob" method

This method opens a TCL channel that can be used to read or write into a preexisting BLOB in the database. The syntax is like this:

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.

glenn jackman
+2  A: 

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...

Jeff Godfrey
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
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
A: 

how to retrieve it back? i try to store a .gif image..but i cant retrieve it back to its original state..i have change translation binary to encoding binary...still not work...

john