pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeroen T. Vermeulen <jtv@xs4all.nl>
To: Roman Bogorodskiy <bogorodskiy@inbox.ru>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: troubles with getting data from tables
Date: Tue, 23 Sep 2003 18:45:04 +0200
Message-ID: <20030923164504.GA70502@xs4all.nl> (raw)
In-Reply-To: <20030923201750.7cacf851.bogorodskiy@inbox.ru>
References: <20030923201750.7cacf851.bogorodskiy@inbox.ru>

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




view thread (3+ messages)  latest in thread

Message-ID: <20030923164504.GA70502@xs4all.nl>
Permalink:  ../20030923164504.GA70502@xs4all.nl/
Also on:    postgresql.org/message-id/20030923164504.GA70502@xs4all.nl

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: jtv@xs4all.nl, bogorodskiy@inbox.ru
  Subject: Re: troubles with getting data from tables
  In-Reply-To: <20030923164504.GA70502@xs4all.nl>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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