Hi,
I am using libpq-fe.h and libpq-fs.h with a postgreSQL 6.4.2 database to
get images in and out the database. lo_import seems to work, but lo_export
does not. I adapted an example to get the following code running:
sprintf( img_name, "'trial.tif'" );
sprintf( sql_query, "SELECT lo_export( image.raster, %s ) from image WHERE
filename = 'image2'", img_name );
res = PQexec( conn, sql_query );
if( PQresultStatus( res ) != PGRES_COMMAND_OK ){sprintf( errbuf, "Query command to database '%s' failed:\n%s",
database,PQerrorMessage( conn ) );PQclear( res );exit_nicely( conn );return im_report_error( "from_to_db", IE_NOT_OK,
errbuf);
}
Anyone ever got "lo_export" running in a C-routine on Red hat Linux 5.2 ?
int from_to_db( IMAGE *in, char *filename, int frto )
{
#ifdef LINUXOid lobjOid = 0;PGconn *conn = NULL;PGresult *res = NULL;
#endifint status = IE_OK;char in_filename[1024], out_filename[1024],
sql_query[1024], img_name[256];char *pghost = NULL, *pgport = NULL, *pgoptions = NULL,
*pgtty = NULL;char *database = NULL;char errbuf[512];
im_begin_func( "from_to_db" );*errbuf = '\0';*in_filename = '\0';*out_filename = '\0';*sql_query = '\0';*img_name =
'\0';
#ifdef LINUXstrcpy( in_filename, filename );sprintf( out_filename, "/images/pvosta/trial.xxx" );pghost =
"blsbl1.janbe.jnj.com"; /* host name of the backend
server */pgport = NULL; /* port of the
backend server */pgoptions = NULL; /* special options
to start up the backend server */pgtty = "/tmp/pgsqlbug.txt"; /* debug_fleging tty or file for the
backend server */database = "linkscil1";
/* Set up the connection */conn = PQsetdb( pghost, pgport, pgoptions, pgtty, database );/* conn = PQsetdb( NULL, NULL,
NULL,NULL, database ); */
/* check to see that the backend connection was successfully made */if( PQstatus( conn ) == CONNECTION_BAD ){
sprintf(errbuf, "Connection to database '%s' failed:\n%s", database, PQerrorMessage( conn ) ); exit_nicely(
conn); return im_report_error( "from_to_db", IE_NOT_OK, errbuf );}
switch( frto ){ case 0: /* Start image import to BLOB */ res = PQexec( conn, "BEGIN" );
PQclear(res ); printf( "Importing image \"%s\" into database
'%s'\n", ImageName( in ), database ); lobjOid = import_image( conn, in );
if( lobjOid == 0 ){ sprintf( errbuf, "lo_import to database '%s'
failed:\n%s", database, PQerrorMessage( conn ) ); exit_nicely( conn ); return
im_report_error("from_to_db",
IE_NOT_OK, errbuf ); } /* Send BLOB to database */ sprintf( sql_query, "INSERT INTO image
(filename,
area, raster ) VALUES ( 'image2', 100, lo_import( '%s' ) )", filename ); res = PQexec( conn, sql_query );
if(PQresultStatus(res) != PGRES_COMMAND_OK ){ sprintf( errbuf, "Query command to database
'%s' failed:\n%s", database, PQerrorMessage( conn ) ); PQclear( res );
exit_nicely(conn ); return im_report_error( "from_to_db",
IE_NOT_OK, errbuf ); }
printf( "\t succeeded as large object %u.\n",
lobjOid ); break; case 1: /* Start file export from BLOB */ printf( "Exporting
image\"%s\" from database
'%s'\n", ImageName( in ), database );
/* Retrieve BLOB from database */
sprintf( img_name, "'trial.tif'" ); sprintf( sql_query, "SELECT lo_export( image.raster,
%s ) from image WHERE filename = 'image2'", img_name ); res = PQexec( conn, sql_query ); if(
PQresultStatus(res ) != PGRES_COMMAND_OK ){ sprintf( errbuf, "Query command to database
'%s' failed:\n%s", database, PQerrorMessage( conn ) ); PQclear( res );
exit_nicely(conn ); return im_report_error( "from_to_db",
IE_NOT_OK, errbuf ); }
/* if( export_image( conn, lobjOid, in ) < IE_OK ){ sprintf( errbuf, "lo_export from database
'%s' failed:\n%s", database, PQerrorMessage( conn ) ); exit_nicely( conn ); return
im_report_error("from_to_db",
IE_NOT_OK, errbuf ); }
*/ printf( "\tsucceeded\n" );
break; default: sprintf( errbuf, "Illegal option for '%s' failed:",
database ); exit_nicely( conn ); return im_report_error( "from_to_db", IE_NOT_OK,
errbuf ); break;}
res = PQexec( conn, "END" );PQclear( res );PQfinish( conn );
if( ( status < IE_OK ) || ( im_get_status() < IE_OK ) ) return im_report_error( "from_to_db", IE_NOT_OK,
"from_to_db
failed" );
#endif
printf( "Finished\n" );
im_end_func( "from_to_db" );
return( IE_OK );
}
Regards,
Peter Van Osta, MD
Biological Imaging Laboratory
Life Sciences Department I - building 160 office 137
Janssen Research Foundation
Beerse, Belgium
tel. +32 (0)14 60.6065
fax.: +32 (0)14 60.5788