pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedtroubles with getting data from tables
3+ messages / 2 participants
[nested] [flat]
* troubles with getting data from tables
@ 2003-09-23 16:17 Roman Bogorodskiy <bogorodskiy@inbox.ru>
0 siblings, 1 reply; 3+ messages in thread
From: Roman Bogorodskiy @ 2003-09-23 16:17 UTC (permalink / raw)
To: pgsql-interfaces
pgsql-interfaces@
I'm writing a backend for some ftp demon on C. I want to make it possible
to authorize server's clients via pgsql. And I have a such problem:
I have a function FCN_VALIDATE_LOGIN which checks if a given users name is ok.
It's looks like this (I've removed some debug code) :
---cut 8<---
int
FCN_VALIDATE_LOGIN (const char *login)
{
PGresult *res;
char *query;
query = malloc(256);
sprintf(query, "SELECT * FROM users WHERE name=\'%s\';", login);
res = PQexec(conn, query);
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "%s\n", PQresultErrorMessage(conn));
printf("Error\n");
}
if (PQntuples(res) == 1) {
int uid_field_index;
unsigned int *uid;
uid_field_index = PQfnumber(res, "uid");
uid = (int *) PQgetvalue(res, 0, uid_field_index);
#ifdef DEBUG
fprintf(stderr, "uid_field_index = %i\n uid %i\n", uid_field_index, uid);
#endif
// return uid;
} else { // Not such user
return -1;
}
PQclear(res);
}
---cut 8<---
And I have a such table:
---cut 8<---
CREATE TABLE users (
name character varying NOT NULL,
passwd character varying,
home character varying,
groups character varying NOT NULL,
rights character varying NOT NULL,
uid int4 NOT NULL,
ip_allowed character varying,
max_dl_speed integer,
bytes_ul_total bigint,
bytes_dl_total bigint,
ratio integer,
flags character varying,
user_slots integer,
leech_slots integer
);
---cut 8<---
I have only one row in this table where uid is '1001'.
wzdftpd=# select uid from users;
uid
------
1001
(1 row)
But instead of '1001' "uid" value is '134820376'. Where I was wrong?
-Roman Bogorodskiy [Novel]
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: troubles with getting data from tables
@ 2003-09-23 16:45 Jeroen T. Vermeulen <jtv@xs4all.nl>
parent: Roman Bogorodskiy <bogorodskiy@inbox.ru>
0 siblings, 1 reply; 3+ messages in thread
From: Jeroen T. Vermeulen @ 2003-09-23 16:45 UTC (permalink / raw)
To: Roman Bogorodskiy <bogorodskiy@inbox.ru>; +Cc: 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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: troubles with getting data from tables
@ 2003-09-24 03:18 Roman Bogorodskiy <bogorodskiy@inbox.ru>
parent: Jeroen T. Vermeulen <jtv@xs4all.nl>
0 siblings, 0 replies; 3+ messages in thread
From: Roman Bogorodskiy @ 2003-09-24 03:18 UTC (permalink / raw)
To: Jeroen T. Vermeulen <jtv@xs4all.nl>; +Cc: pgsql-interfaces
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jeroen wrote:
> 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.
I'm new to libpq programming, so...
> 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!
Thank you fo answer, it realy helped me.
- -Roman Bogorodskiy [Novel]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)
iQEVAwUBP3ENESpMDQ8aPhy0AQKvNQf/RcCNMoCDQiTeCBecZ5XcI1IS3QVfReiN
UuvPD54081AZNtLq2H+Ux7XRHeacwaSDna2l5oXLZCrVsmbrD8XdzbZoK/CB0Ry0
j8ansMkFJmhaB2FGiK/7UG7Ov82qGi6Q3vMxVWZajmSd00qafs442/RT4+KJsVvy
r4PHvHoTDRHmh/PAMFfXABHJE5eub9D9NmL6ETp0HDCHMvVa26nUpProPkZF6Wz9
GyKcVOEU7xgKyoFsr6Zr/o6qXfNYtAjttewzrMXjTkwtN51EJnOA0gHXjlrKt3TU
yiiM+J2euk6V/90qCvGvZc4trw49Sno2rpWI4JfReu3AECaEWIAe2g==
=7SKo
-----END PGP SIGNATURE-----
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2003-09-24 03:18 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-09-23 16:17 troubles with getting data from tables Roman Bogorodskiy <bogorodskiy@inbox.ru>
2003-09-23 16:45 ` Jeroen T. Vermeulen <jtv@xs4all.nl>
2003-09-24 03:18 ` Roman Bogorodskiy <bogorodskiy@inbox.ru>
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