I have a blob column in my table. How do I insert an image into it and how do I retrieve it onto the picturebox on a windows form?
+6
A:
I recommend that you don't do this. Store it as a file on the filesystem and put the filename in the database instead.
Ignacio Vazquez-Abrams
2010-01-16 11:42:15
For the edification of myself and others, why is storing the image as a blog in the db a bad idea? Is this just a MySQL issue?
Shoko
2010-01-16 13:52:43
There are database engines out there that handle largish BLObs in a manner that doesn't hurt performance, but MySQL isn't one of them.
Ignacio Vazquez-Abrams
2010-01-16 19:25:25
A:
- It is probably best if you store your images in the filesystem instead of the database.
- but if you want to store in DB, you can just write the image data in as string and then retrieve it back as a string, set a header depending on the image type and then just flush it to the browser. (I did that using php and it worked)
meosoft
2010-01-16 11:44:24
Hope I will have to store my images in the file system. thank you Meosoft
Albert
2010-01-16 14:13:52
+2
A:
If the image is located on your MySQL server host, you could use the LOAD_FILE()
command:
INSERT INTO my_table (image_col) VALUES(LOAD_FILE('/tmp/my_image.png'));
Make sure that the file is readable by MySQL, and also make sure that your MySQL user has the FILE
privilege.
To grant the FILE
privilege, log in as root and execute:
GRANT FILE ON *.* TO 'mysql_user'@'localhost';
In general I would also recommend storing the image on the filesystem, and just keeping the path in MySQL. However there are cases where this is useful.
Daniel Vassallo
2010-01-16 12:03:46
Thanks for your advice Daniel. Currently am storing the path to the image but I want to be able to move the database and application to another computer without worrying about the path to the images.
Albert
2010-01-16 13:02:14