Fred J. Stephens wrote:
> For instance, how can I store a file in a table?
> Not read the file and store the text, but the binary file itself?

Fred,

You can't do anything with the contents of a file until you read in into 
memory.

To store a 1MB file in a database you need to decide if you will use the 
older complete blob API (i.e. sqlite3_bind_blob and sqlite3_column_blob) 
or the newer incremental I/O API routines sqlite3_blob_open, 
sqlite3_blob_write, and sqlite3_blob_close). The main difference is the 
size of the memory buffer needed to hold the data, and the number of 
times you call the routines.

For a 1MB file you need a 1MB memory buffer to use the old API. You will 
read the entire file into the buffer and call the bind_blob function 
once to insert the data into the database.

If you use the new API's you would first insert a 1MB zeroblob (i.e. 1M 
of zero data) using the sqlite3_bind_zeroblob function. Next you open 
the blob using sqlite3_open_blob. Then you could use a 10K buffer, and 
call the file read and write_blob functions 100 times in a loop to 
transfer the file into the database. Finally you close the blob.

The newer APIs are more complicated to use in that you need to make more 
function calls to insert the blob, but they allow the application to use 
less memory, and they can handle data that is larger than what will fit 
in memory (i.e. it is the only way to insert a 100G file on a machine 
with only 2G of memory).

HTH
Dennis Cote
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to