// G ++ gettablefieldnum. cpp pqcopy. cpp-I/usr/local/pgsql/include/-L/usr/local/pgsql/lib-lpq-o get-G
# Include <libpq-fe.h>
# Include <iostream>
# Include "pqcopy. H"
Using namespace STD;
Char pdbserver [str_len + 1];
Char g_strdbname [str_len + 1];
Char pdbuser [str_len + 1];
Char pdbpwd [str_len + 1];
Int g_nclientencoding;
Bool gettablecolumn (char * par_strtablename, Int & par_uscolumnno)
{
Pgconn * pdbconn = NULL;
Pdbconn = connectdb ();
Int status =-1;
Pgresult * res = NULL;
Char Psql [str_len + 1] = {0 };
Snprintf (Psql, str_len, "select * From td_usergrp_waphost_d_20120416 limit 1 ;");
Res = pqexec (pdbconn, "begin ");
If (pqresultstatus (RES )! = Pgres_command_ OK) // a command that does not return data is successfully completed.
{
// Log ("begin failed [% s]", pqerrormessage (pgconn ));
}
Pqclear (RES );
Res = pqexec (pdbconn, Psql );
Status = pqresultstatus (RES );
If (status! = Pgres_tuples_ OK) // a query of the returned data is successfully executed.
{
Log ("failed to query the table ");
Char * pstrerror = pqerrormessage (pdbconn );
Printf ("SQL: % s \ n", pstrerror );
Pqclear (RES );
}
Par_uscolumnno = pqnfields (RES );
Cout <pqnfields (RES) <Endl;
Pqclear (RES );
Res = pqexec (pdbconn, "end ");
Pqclear (RES );
}
Int main ()
{
Strncpy (pdbserver, "192.168.227.128", str_len );
Strncpy (g_strdbname, "test", str_len );
Strncpy (pdbuser, "Postgres", str_len );
Strncpy (pdbpwd, "123456", str_len );
Char strtable [100];
Int uscolumnno = 0;
Gettablecolumn (strtable, uscolumnno );
Cout <uscolumnno <Endl;
}