The way to insert image file into postgresql


Read With Formatting | Free Open Source Tutorials Account

PostgreSQL
Thread: The way to insert image file into postgresql


wingchun
Hallo to all,
I have a problem,
i have a java application (stand alone) and it works with Postgresql.
My problem is that i'm not able to put(and read and so on...) an image file into a table of Postgresql.
So i wish to know wich is the best way to do this.
Thanks.

md_doc
I would think you would need to read the file in as a binary file and save it as a binary type in the database then when you take it back out pretend the buffer is a pointer to the file data and just show it.

I know it is easier said than done but as to the exact steps you have to take with java I am not sure :( Unless you just save the image somewhere as a flat file and store the location in pgsql.

PriceComparison
I am not at liberty to just cut-and-paste my code from work. But I can give some clues and direction. Unfortunately it is not just as easy as just "...just show it..." heehehe... that was funny.

First you have to store the image data as binary (BLOB). Then when retreiving it comes back as one large string. This is the clue. Use a PHP function to redirect output to a certain file type such as GIF or JPEG.

This is demonstrated at http://www.PriceComparison.com. All of our image files are generated by a PHP script which pulls from database.

Good Luck

Andrew - DBA for http://www.PriceComparison.com

md_doc
That is very true. I did not even think about it.

Take a php script that takes an argument via the get param... image_id or something. Have that call the database get the information from the BLOB and then set the header to a gif or jpg or whatever kind of image it is then just echo out the content. It should display just fine.