pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Help with returning data type
3+ messages / 3 participants
[nested] [flat]

* Help with returning data type
@ 2005-02-16 13:06  Rudinei Dias <rudinei@unilasalle.edu.br>
  0 siblings, 1 reply; 3+ messages in thread

From: Rudinei Dias @ 2005-02-16 13:06 UTC (permalink / raw)
  To: pgsql-interfaces

Help with returning data type

Hi.

I'm in trouble with data type returns using libpq (PQgetvalue)

see table definition
-  datname name NOT NULL,
-  encoding int4 NOT NULL,
-  datistemplate bool NOT NULL,
-  datallowconn bool NOT NULL,

I codify with this way in C

  static long long_ret;
  static u_long ulong_ret;
  static unsigned char string[SPRINT_MAX_LEN];
  static oid objid[MAX_OID_LEN];
  static struct counter64 c64;
  int i, index;
  char* bytea;

*       // 1 - THIS WORKS (retrieving varchar)
*
        strcpy(query_string, "SELECT datname FROM pg_database limit 1 
offset 0");
        res = PQexec(conn, query_string);                 
        if (PQresultStatus(res) != PGRES_TUPLES_OK){
            fprintf(stderr, "SELECT query failed [%s].\n", query_string);
            PQclear(res);
            return;
        }
        i = 0;
        bytea = PQgetvalue(res, i, 0);
        PQclear(res);
        strcpy(string, bytea);
        *var_len = strlen(string);
        return (unsigned char *) string;

*       // 2 - THIS WORKS (retrieving int4)
*
        strcpy(query_string, "SELECT encoding FROM pg_database limit 1 
offset 0");
        res = PQexec(conn, query_string);                 
        if (PQresultStatus(res) != PGRES_TUPLES_OK){
            fprintf(stderr, "SELECT query failed [%s].\n", query_string);
            PQclear(res);
            return;
        }
        i = 0;
        bytea = PQgetvalue(res, i, 0);
        index = atoi(bytea);
        PQclear(res);
        switch (index){
        case 0: strcpy(string, "SQL_ASCII"); break;
        case 1: strcpy(string, "EUC_JP"); break;
        ...
        case 33: strcpy(string, "GB18030"); break;
        }
        *var_len = strlen(string);
        return (unsigned char *) string;

*       // 3 - THIS **DONT **WORK (retrieving bool)
***      
        long_ret = 0;
        strcpy(query_string, "SELECT datistemplate FROM pg_database 
limit 1 offset 0");
        res = PQexec(conn, query_string);                 
        if (PQresultStatus(res) != PGRES_TUPLES_OK){
            fprintf(stderr, "SELECT query failed [%s].\n", query_string);
            PQclear(res);
            return;
        }
        i = 0;
        bytea = PQgetvalue(res, i, 0);
        long_ret = atol(bytea);
       
        return (unsigned char *) &long_ret;

How can I retrieve a bool data type (code 3)?
How can I return for a string C data type (return (unsigned char *) 
&long_ret;)
and the last question
although working, its correct list code 1 and list code 2?
anybody has code sample to share (mail me)?

thank you for any help, my deadline paper is near and has proposed the 
open source MIB for postgresql database!
thank you!



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Help with returning data type
@ 2005-02-16 23:41  Michael Fuhr <mike@fuhr.org>
  parent: Rudinei Dias <rudinei@unilasalle.edu.br>
  0 siblings, 0 replies; 3+ messages in thread

From: Michael Fuhr @ 2005-02-16 23:41 UTC (permalink / raw)
  To: Rudinei Dias <rudinei@unilasalle.edu.br>; +Cc: pgsql-interfaces

On Wed, Feb 16, 2005 at 10:06:58AM -0300, Rudinei Dias wrote:
>
> *       // 3 - THIS **DONT **WORK (retrieving bool)
>         ...
>        bytea = PQgetvalue(res, i, 0);
>        long_ret = atol(bytea);

What's the value of bytea (the string bytea points to)?
What output were you expecting?
What output did you get?

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Help with returning data type
@ 2005-02-17 00:50  Janet Borschowa <borschow@roguewave.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Janet Borschowa @ 2005-02-17 00:50 UTC (permalink / raw)
  To: Rudinei Dias <rudinei@unilasalle.edu.br>; pgsql-interfaces

Hi,
This isn't working because the char* you are getting back from PQgetvalue is
either 't' or 'f' and so atol() fails. Also, you should not being calling
PQclear on the PGresult until you are done with it and all the data it
returned via PQgetvalue:

        i = 0;
        bytea = PQgetvalue(res, i, 0);
        //PQclear(res); // don't call this here because you are using bytea
in the next call!
        strcpy(string, bytea);
        PQclear(res); // call it here!
        *var_len = strlen(string);
        return (unsigned char *) string;

One other point, the table's schema should not use the datatype "name" as
this is datatype is only for storage of internal catalog names. It would be
much better to use "varchar(64)".

========================
Janet Borschowa
Software Engineer, Database Products                       
Rogue Wave Software, a QUOVADX(tm) Division
(541) 753-1931   FAX: (541) 757-4630
mailto:borschow@roguewave.com  http://www.roguewave.com 




-----Original Message-----
From: Rudinei Dias [mailto:rudinei@unilasalle.edu.br] 
Sent: Wednesday, February 16, 2005 5:07 AM
To: pgsql-interfaces@postgresql.org
Subject: [INTERFACES] Help with returning data type


Help with returning data type

Hi.

I'm in trouble with data type returns using libpq (PQgetvalue)

see table definition
-  datname name NOT NULL,
-  encoding int4 NOT NULL,
-  datistemplate bool NOT NULL,
-  datallowconn bool NOT NULL,

I codify with this way in C

  static long long_ret;
  static u_long ulong_ret;
  static unsigned char string[SPRINT_MAX_LEN];
  static oid objid[MAX_OID_LEN];
  static struct counter64 c64;
  int i, index;
  char* bytea;

*       // 1 - THIS WORKS (retrieving varchar)
*
        strcpy(query_string, "SELECT datname FROM pg_database limit 1 
offset 0");
        res = PQexec(conn, query_string);                 
        if (PQresultStatus(res) != PGRES_TUPLES_OK){
            fprintf(stderr, "SELECT query failed [%s].\n", query_string);
            PQclear(res);
            return;
        }
        i = 0;
        bytea = PQgetvalue(res, i, 0);
        PQclear(res);
        strcpy(string, bytea);
        *var_len = strlen(string);
        return (unsigned char *) string;

*       // 2 - THIS WORKS (retrieving int4)
*
        strcpy(query_string, "SELECT encoding FROM pg_database limit 1 
offset 0");
        res = PQexec(conn, query_string);                 
        if (PQresultStatus(res) != PGRES_TUPLES_OK){
            fprintf(stderr, "SELECT query failed [%s].\n", query_string);
            PQclear(res);
            return;
        }
        i = 0;
        bytea = PQgetvalue(res, i, 0);
        index = atoi(bytea);
        PQclear(res);
        switch (index){
        case 0: strcpy(string, "SQL_ASCII"); break;
        case 1: strcpy(string, "EUC_JP"); break;
        ...
        case 33: strcpy(string, "GB18030"); break;
        }
        *var_len = strlen(string);
        return (unsigned char *) string;

*       // 3 - THIS **DONT **WORK (retrieving bool)
***      
        long_ret = 0;
        strcpy(query_string, "SELECT datistemplate FROM pg_database 
limit 1 offset 0");
        res = PQexec(conn, query_string);                 
        if (PQresultStatus(res) != PGRES_TUPLES_OK){
            fprintf(stderr, "SELECT query failed [%s].\n", query_string);
            PQclear(res);
            return;
        }
        i = 0;
        bytea = PQgetvalue(res, i, 0);
        long_ret = atol(bytea);
       
        return (unsigned char *) &long_ret;

How can I retrieve a bool data type (code 3)?
How can I return for a string C data type (return (unsigned char *) 
&long_ret;)
and the last question
although working, its correct list code 1 and list code 2?
anybody has code sample to share (mail me)?

thank you for any help, my deadline paper is near and has proposed the 
open source MIB for postgresql database!
thank you!

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-02-17 00:50 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-02-16 13:06 Help with returning data type Rudinei Dias <rudinei@unilasalle.edu.br>
2005-02-16 23:41 ` Michael Fuhr <mike@fuhr.org>
2005-02-17 00:50 Re: Help with returning data type Janet Borschowa <borschow@roguewave.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox