Thread: help !!!

help !!!

From
"apellido jr., wilfredo p."
Date:
Hello! im a newbie in C programing as well as Postgresql. The above example
is workin but im having difficulty regarding assigning the result of Query
Select.  here's the program
       int       main()       {           char        UserName[50];                          /* holds user
state code */           char        query_string[256];                      /* holds
constructed SQL query */           PGconn     *conn;                                   /* holds
database connection */           PGresult   *res;                                    /* holds
query result */
           conn = PQconnectdb("dbname=grade");                  /* connect
to the database */
           if (PQstatus(conn) == CONNECTION_BAD)               /* did the
connection fail? */           {               fprintf(stderr, "Connection to database failed.\n");
fprintf(stderr,"%s", PQerrorMessage(conn));               exit(1);           }
 
           printf("Enter Username:  ");                    /* prompt user
for a state code */           scanf("%s", UserName);
           sprintf(query_string,                               /* create an
SQL query string */                   "SELECT username, value                    FROM table1                    where
test= 'grade' and username = '%s'", UserName);
 
           res = PQexec(conn, query_string);                   /* send the
query */
           if (PQresultStatus(res) != PGRES_TUPLES_OK)         /* did the
query fail? */           {               fprintf(stderr, "SELECT query failed.\n");               PQclear(res);
     PQfinish(conn);               exit(1);           }
 
           printf("\n\n\n");           printf("Username :  %s\n\n\n", PQgetvalue(res, 0, 0));           printf("Grade :
%s \n\n\n", PQgetvalue(res, 0, 1));
 
           PQclear(res);                                       /* free
result */
           PQfinish(conn);                                     /*
disconnect from the database */
           return 0;       }

############################################################################
########


hello how can i assign to certain variable the output of PQgetvalue(res, 0,
1)? example i want to do like this ...

a = PQgetvalue(res, 0, 1)
printf("Grade : %s  \n\n\n", a );

and then after assigning the value of PQgetvalue(res, 0, 1) to a then ...

b = a \ 2;

but  it is possible to divide the output of "a" where it is a var and "b" is
integer?













Re: help !!!

From
"Goulet, Dick"
Date:
PGgetValue returns a character pointer, so if you have:
char *reply_string = NULL;
reply_string = PQgetResult(rex,0,0);

Dick Goulet
Senior Oracle DBA
Oracle Certified 8i DBA

-----Original Message-----
From: apellido jr., wilfredo p. [mailto:apellido@mactan.ph]
Sent: Wednesday, June 30, 2004 9:26 AM
To: pgsql-interfaces@postgresql.org
Subject: [INTERFACES] help !!!


Hello! im a newbie in C programing as well as Postgresql. The above example
is workin but im having difficulty regarding assigning the result of Query
Select.  here's the program
       int       main()       {           char        UserName[50];                          /* holds user
state code */           char        query_string[256];                      /* holds
constructed SQL query */           PGconn     *conn;                                   /* holds
database connection */           PGresult   *res;                                    /* holds
query result */
           conn = PQconnectdb("dbname=grade");                  /* connect
to the database */
           if (PQstatus(conn) == CONNECTION_BAD)               /* did the
connection fail? */           {               fprintf(stderr, "Connection to database failed.\n");
fprintf(stderr,"%s", PQerrorMessage(conn));               exit(1);           } 
           printf("Enter Username:  ");                    /* prompt user
for a state code */           scanf("%s", UserName);
           sprintf(query_string,                               /* create an
SQL query string */                   "SELECT username, value                    FROM table1                    where
test= 'grade' and username = '%s'", UserName); 
           res = PQexec(conn, query_string);                   /* send the
query */
           if (PQresultStatus(res) != PGRES_TUPLES_OK)         /* did the
query fail? */           {               fprintf(stderr, "SELECT query failed.\n");               PQclear(res);
     PQfinish(conn);               exit(1);           } 
           printf("\n\n\n");           printf("Username :  %s\n\n\n", PQgetvalue(res, 0, 0));           printf("Grade :
%s \n\n\n", PQgetvalue(res, 0, 1)); 
           PQclear(res);                                       /* free
result */
           PQfinish(conn);                                     /*
disconnect from the database */
           return 0;       }

############################################################################
########


hello how can i assign to certain variable the output of PQgetvalue(res, 0,
1)? example i want to do like this ...

a = PQgetvalue(res, 0, 1)
printf("Grade : %s  \n\n\n", a );

and then after assigning the value of PQgetvalue(res, 0, 1) to a then ...

b = a \ 2;

but  it is possible to divide the output of "a" where it is a var and "b" is
integer?












---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Re: help !!!

From
"Nigel J. Andrews"
Date:
On Wed, 30 Jun 2004, Goulet, Dick wrote:
> PGgetValue returns a character pointer, so if you have:
> 
>     char *reply_string = NULL;
> 
>     reply_string = PQgetResult(rex,0,0);

Never used the C interface so I'll assume getValue is the function name...

> 
> hello how can i assign to certain variable the output of PQgetvalue(res, 0,
> 1)? example i want to do like this ...
> 
> a = PQgetvalue(res, 0, 1)
> printf("Grade : %s  \n\n\n", a );

That was answered above.

> 
> and then after assigning the value of PQgetvalue(res, 0, 1) to a then ...
> 
> b = a \ 2;
> 
> but  it is possible to divide the output of "a" where it is a var and "b" is
> integer?

Sure. The complexity depends how safety conscious you are.


double d;
char *s;

s = PQgetValue(...);

d = strtod(s,NULL) / 2.0;

will give zero or a perhaps unexpected result if s points to something that
isn't convertable to a double in it's entirety. Read up on strtod() but
basically, "abcde56.98" will give 0 and "56.98abcde" will give 56.98. You
supply a second argument to strtod, as below, to determine how successful the
conversion was.


char *end;

d = strtod(s,&end);
if (end != s+strlen(s))
{/* string incompletely converted */
}

or the above test could be replaced with if (*end) {... which is how I'd write
it.

Then there's also checks for under and overflow:

errno=0;
d=strtod(s,&end);
if (d == 0 && errno == ERANGE) { /* underflow */ }
if ((d == HUGE_VAL || d == -HUGE_VAL) && errno == ERANGE) { /* overflow */ }

I'm pretty sure that errno should not be set if there is no under or overflow
so those tests can be adjusted to take advantage of that fact.

Hope that helps. Remember, man pages are your friends and man -k can be a
useful starting point if you can think of a a keyword. Consider getting a book
on C, sorry can't recomend one, and/or searching the web for tutorials.


--
Nigel Andrews





Re: help !!!

From
"apellido jr., wilfredo p."
Date:
Hello Sir, thanks for reply.  Im just testing what you said and i got thiserror:

Enter Username:  apellidoUsername :  apellidoGrade : (null)

-----------------------------my code:


double    i;
char       *reply_string;

reply_string = PQgetvalue(res, 0, 1); i = strtod(reply_string,NULL)/2.0;


Sir, i know that PQgetvalue(res, 0, 1); will return number although it isdeclare in the field varchar. Im already test

int i;                (also  tried unsigned short int)

reply_string = PQgetvalue(res, 0, 1);i = strtoul(reply_string,NULL, 3)/2.0;

Still i got the same error...  Im googling how to convert ascii to integerand i saw another question which confused me:
hereit is
 



Is there a command that can be used to temporarily convert a string value ina field to integer?

I.E.

SELECT name, employeeidFROM usersWHERE employeeid = bob

In this case employeeid is an integer, and bob is a number but stored as astring.  how do you temporarily convert bob
toan integer for the sake ofthe comparison?