0

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
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?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.