C++에서 MySQL에 이미지 넣기

tmvlemaos의 이미지

C++로 MySQL에 이미지를 저장해보려고 하는데요

잘안되서 질문올립니다.

현재 MySQL table은

+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| imageName | varchar(128) | NO | PRI | | |
| length | mediumint(9) | NO | | 0 | |
| image | mediumblob | NO | | | |
+-----------+--------------+------+-----+---------+-------+

이렇게 구성되어있구요.

이미지를 열고 읽은다음에 바로 다른 이름으로 저장하면 제대로 되는데

디비에 넣고난 후에 디비에서 읽어들인후 파일로 생성하면

제대로 되질 않습니다.

개발환경은 Redaht Linux Enterprize Server 3 입니다.

제가 구성한 코드는 아래와 같습니다.

Code :

//이미지를 DB에 넣는 부분

//InsertImage

void ImageDBMGR::InsertImage( string filename )
{

//Get file length
long length = this->GetFileSize( filename );

//File open
int fileOpen = open( const_cast( filename.c_str() ), O_RDONLY );

char* image = 0;

if( fileOpen != -1 )
{
//Create char type array at heep space
image = new char[length];

//Read the file as length byte
read( fileOpen, image, length );
}

//Close the file
close( fileOpen );

//Insert Image at Database
try {

//Open the databasei
Connection con( "FreemanTest", "localhost", "root", "dreams" );

//Declaration Query Variable
Query query = con.query();

//Set Query Statement
query ( filename.c_str() )

query.parse();

query.execute();
}
catch( const BadQuery& er )
{
cerr }

}

//이미지를 DB에서 가져오는 부분

//GetImage
void ImageDBMGR::GetImage( string filename )
{
//Open the database
Connection con( "FreemanTest", "localhost", "root", "dreams" );

//Declaration Query Variable
Query query = con.query();

//Set Query Statement
query ( filename.c_str() )

//Query Excute, and Store the date at Result Variable
Result res = query.store();

int count = res.num_rows();

if( count != 0 )
{
//Store the data at first ow
Row row = res.fetch_row();

Row::size_type i;

string image;
int length = 0;

try {
//Loop for read Result Set
for( i = 0; row = res.at(i); ++i )
{
length = atol( const_cast( row.raw_data(1) ) );

cout

//Open the file
int imageFile = open( const_cast( filename.c_str() ), O_RDWR | O_CREAT | O_TRUNC, length );

if( imageFile != -1 )
{
write( imageFile, const_cast( row.raw_data(0) ), length );
}

close( imageFile );

}
}
catch( const BadQuery& er )
{
cerr }
catch( const EndOfResults& end )
{
}

}

}

뭐가 문제일까요..

cmoh1110의 이미지

query를 주고 받을 때는 binary 모드로 하나요?