Skip site navigation (1) Skip section navigation (2)

Re: troubles with getting data from tables

From: "Jeroen T(dot) Vermeulen" <jtv(at)xs4all(dot)nl>
To: Roman Bogorodskiy <bogorodskiy(at)inbox(dot)ru>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: troubles with getting data from tables
Date: 2003-09-23 16:45:04
Message-ID: 20030923164504.GA70502@xs4all.nl (view raw or flat)
Thread:
Lists: pgsql-interfaces
On Tue, Sep 23, 2003 at 08:17:50PM +0400, Roman Bogorodskiy wrote:
 
>         sprintf(query, "SELECT * FROM users WHERE name=\'%s\';", login);
 
>         if (PQntuples(res) == 1) {
>                 int uid_field_index;
>                 unsigned int *uid;
> 
>                 uid_field_index = PQfnumber(res, "uid");

First off, a question: why don't you just select the field you want, so
you know its index in the result set?  That'll probably transport less 
data, save you that PQfnumber() call, and make your code shorter and
more readable.


>                 uid = (int *) PQgetvalue(res, 0, uid_field_index);

> But instead of '1001' "uid" value is '134820376'. Where I was wrong?

The pointer you're getting is a pointer-to-char, i.e. a string.  Unless
you explicitly enabled the "binary tuples" option, what you're getting is
exactly that: a string.  But instead of parsing the string, you're
casting its base pointer to pointer-to-int!


Jeroen


In response to

Responses

pgsql-interfaces by date

Next:From: Ryan MooneyDate: 2003-09-23 21:12:07
Subject: ECPG insert into table name as a variable
Previous:From: Roman BogorodskiyDate: 2003-09-23 16:17:50
Subject: troubles with getting data from tables

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group