Avatar of clodo13
clodo13 asked on

store image on postgres base with odbc

I have a postgres database and my aim is to save a JPG file from disk for exemple (d:/file.jpg)
to a postgres database (bytea column)

First i want to create a CbyteArray of this image then insert ....in databse.

System ProgrammingPostgreSQL

Avatar of undefined
Last Comment

8/22/2022 - Mon

1) Create a DSN to the database
2) Be sure that
      #include <afxdb.h>
    is in your StdAfx.h file.

3) Use the class wizard to create a CRecordset-derived object (CRsPicRec)
    that accesses the db table

4) Modify the RsPicRec.cpp file as follows:
      m_nIdx = 0;
      m_sName = _T("");
      // m_imgBinaryData = _T("");  // comment out this line;
      // RFX_Text(pFX, _T("[imgBinaryData]"), m_imgBinaryData);
      RFX_Binary(pFX, _T("[imgBinaryData]"), m_imgBinaryData);

3) Modify the RsPicRec.h file as follows:

      // CString            m_imgBinaryData;
      CByteArray      m_imgBinaryData;
4) Use code like that below to open the recordset (selecting no items in the Open) and to do the Update() call to send the data to the database.

Write your own code to learn the length of the file and to read the file data into the buffer.
#include "RsPicRec.h"
void CD46Dlg::OnButton1() 
	int nLen=5000;  // obtain the file length
	CRsPicRec crs;
	int n= crs.Open();
	BYTE* p= (BYTE *)crs.m_imgBinaryData.GetData(); // learn buffer location
	// read the file data into the buffer at p (not shown)
	strcpy ( (char*)p, "hi there" );  // just to verify the data gets to the db
	//----------------------- populate the other fields
	crs.m_nIdx= 124;
	n= crs.Update();

Open in new window


I tried the code but the binary column is not saved in base
when I calculate the size of the image I find column 1 byte

i read the data in buffer like this.:

thanks for help

 CFile strFile;
 FileLth = strFile.GetLength();
// read the file data into the buffer at p 

Open in new window


First, breakpoint and use the debugger to verify the value in the FileLth variable.

Next, try it with a very short file (say 10 bytes) then a longer one (100 bytes), etc.  Each time, check that the transfer to the database occurred.  

For these diagnostic tests, use a .Txt file created in Notepad -- that way you can see the data as text rather than binary gibberish.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

First thank you for help, :=)
The value of FileLth is ok ! the same length of file
i try with a (small land .TXT) file and no data in the binary column :=(
I think there is a probleme between buffer(p) and the binary column
I check the buffer p with debugger and it contains data !
but after upadate no data in base :=(

other ideas  :)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I tried to put data like that directly with the recordSet variable
crs.m_imgBinaryData.SETAT( <-- this methode work but i have (unhundled expetion after crs.update())
The insert work but with unhundled exception.

Other ideas

Thank for Your help.

Both techniques work fine for SQL Server and IBM DB2.
If you've tried the BeginTrans/CommitTrans idea, then I'm about out of ideas.  

I suppose that you could try ADO access... but since it probably uses the same mechanisms of the underlying driver, it's not to likely to work any better.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Thank you Vm for help
I will try again all these methode to insert the image ...........hop that will work :))


when i use  

       cDb.ExecuteSQL( cDb.m_sStmt );

For text is ok !
but with image i inset juste 4 first BYTE  --->IN BASE ("\377\330\377\340" ) <--- JPG CODE
but the rest of the image is not inserted

ideas :)

If a lengthy text file works, but a same-length binary file does not work, that almost always means that the binary file contains a NUL (binary 0) embedded.   Many functions that are used to determin the lenght of a string stop counting when they reach the first NUL.

Visual Studio lets you examin a file as binary data, displayed in hexidecimal.  Use it to examine that same JPG file.  My bet is that you will discover a byte of 00 at byte 5.
Your help has saved me hundreds of hours of internet surfing.