views:

35

answers:

3

I have an sqlite table w/ 500 records each with images stored as blob data. The images are all large size and I need to make thumbnail sized versions of each image and then save them in a separate field.

Does anyone know what would be the optimum way of approaching this task?

One other question - is it recommended to have a separate table to store the images?

thanks in advance

+1  A: 

As Jauzsika said don't store image on database, howeever if you have database with images you could loop over all tables and check which column is BLOB, then just loop over all rows - get data from cell and you have image (ok, data - but it easy to create image when you got data) ;-)

i know, my english grammer is brilliant.

check: http://www.sqlite.org/faq.html#q7

nilphilus
+1  A: 

The easiest way is to first do a query to get all of the row keys to process, but no BLOB data. Then iterate over that list (not in SQL) and fetch the BLOB data from the particular row, generate the thumbnail by whatever means, and then write it back to the thumbnail column for the row. It may take some time, but at least you won't have to worry about hitting memory limits or other such frustrating nonsense. It's a good idea to do this when you can be sure that nothing else is using the DB so you can avoid having to worry about transactions, and once you've done it once, update your row insert/update code so that thumbnails are kept up to date during processing rather than as a separate batch step.

As for whether the images should be in the DB, that's a good one. The thumbnails quite possibly should be, but the image data itself could be fairly large (especially with more rows) so perhaps should live as separate files with only the filenames in the DB. You need to balance the ability to normally process the data faster (by not having all that image data in there) against the ability to keep all the data consistent (too easy to get into problems if something is poking around in the image data). I can't really answer that for you as it requires deeper knowledge of the application – it's your job – but I hope this tells you what you should be thinking about when making the decision.

Donal Fellows
A: 

Just to confirm:

CREATE TABLE "pictures" ("id" INTEGER PRIMARY KEY AUTOINCREMENT,
"picture" TEXT);
INSERT INTO "pictures" ("picture") VALUES ("avatar001.png");

Then in Objective C when I retrieve the record, I get the Documents directory path and then just prepend that before the "picture" field?

zardon