PostgreSQL obtains the number of columns in a specified table.

Source: Internet
Author: User
Tags psql

// 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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.