#include <stdio.h>
#include <tchar.h> // on Vista
#include <libpq-fe.h> // from: the postgres 8.4 install include directory, for me...
D:\Programs\PostgreSQL\8.4\include
// configure these constants and get a '0' even though the same query produces a '1' in pgAdmin
const char* pgUser = "us"; // PG user
const char* pgDbms = "db"; // database
const char* pgPass = "xyz"; // password
const char* pgHost = "localhost"; // host domain or IP
const char* pgTable = "tableName"; // a table that exists in the pgDbms
// on UNIX you can obviously revert the main() declaration to main( int argc, char** argv )
int _tmain( int argc, _TCHAR* argv[] )
{ char connInfo[128];
sprintf( connInfo, "host=%s dbname=%s user=%s password=%s", pgHost, pgDbms, pgUser, pgPass );
PGconn* conn = PQconnectdb( connInfo );
if ( PQstatus(conn) == CONNECTION_OK )
{ // in my case... SELECT COUNT(*) FROM information_schema.tables WHERE table_name='proxies'
sprintf( connInfo, "SELECT COUNT(*) FROM information_schema.tables WHERE table_name='%s'",
pgTable );
PGresult* res = PQexec( conn, connInfo );
if ( res )
{ if ( PQresultStatus(res) == PGRES_TUPLES_OK )
{ int nTuples = PQntuples(res);
int nFields = PQnfields(res);
if ( nTuples > 0 && nFields > 0 )
{ char* val = PQgetvalue(res,0,0); // get first column, first field
fprintf( stderr, "val=%s\n", val );
}
}
PQclear( res ); // possibly moot
res = NULL;
}
PQfinish( conn );
}
return 0;
}
//--------------------------------------------------------------------
// you'll need to link with.libpq, in my case, that's: D:\Programs\PostgreSQL\8.4\lib\libpq.lib