dbTalk Databases Forums  

PQSQL - binary data question

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss PQSQL - binary data question in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bastian Voigt
 
Posts: n/a

Default PQSQL - binary data question - 10-29-2004 , 07:09 AM






Hallo List!

I just found this old posting on google. Now my question is how can I read an
integer value from the PGresult using the binary format? Can someone plz
gimme a code example? (please mail to me, because I have not subscribed to
the list)

Thanks a bunch! Now here's the old posting:

On Monday 27 October 2003 09:15, Tomasz Myrta wrote:
Quote:
Dnia 2003-10-27 00:08, UÅytkownik creid napisaÅ:
Problem: Assigning a COUNT(*) result into an integer variable in my C
program consistently fails except when I assign the same result to a char
variable. I can only assume that the internal data type the COUNT
function uses is integer.

Can anyone help put me in the proper mindset so I may deal with this,
seemingly simple issue, to resolution.

I need the integer result to to help me satisfy a dynamic memory
requirement... COUNT(*) result will tell me how many rows of data I need
to malloc and I cannot perform a math operation on a char variable.

All libpq results are strings.
some_int_value=atoi(PQgetvalue(...))
Not true anymore with protocol v3, which added the binary format. Text format
is still the default.

Quote:
Anyway why do you need count(*) ? When you retrieve your rows, you can
always check how many are them using PQntuples(...) and then malloc your
memory tables.

Regards,
Tomasz Myrta
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #2  
Old   
David Helgason
 
Posts: n/a

Default Re: PQSQL - binary data question - 11-01-2004 , 01:16 AM






On 29. okt 2004, at 14:09, Bastian Voigt wrote:
Quote:
I just found this old posting on google. Now my question is how can I
read an
integer value from the PGresult using the binary format? Can someone
plz
gimme a code example? (please mail to me, because I have not
subscribed to
the list)
Easy peasy:

/* (in C++, actually would give simpler code in C) */

// To submit the query
bool PgConn::SendPrepared(const string& name, const vector<const
char*>& values, const vector<int>& lengths, const vector<int>&
isBinary) {
if (values.size() != lengths.size() || values.size() !=
isBinary.size())
return Error("PgConn::SendPrepared: All parameter arrays must have
same size");

// for (int i = 0; i != values.size(); i++)
// printf ("Query parameter %d, length %d, binary %d: '%s'\n", i,
lengths[i], (int)isBinary[i], values[i]);

if (! PQsendQueryPrepared(m_Conn, name.c_str(), values.size(),
&values.front(), &lengths.front(), &isBinary.front(), 1 /* want binary
result */))
return Error();

return Success();
}


/* ... then after reading the PGresult */

static int NetworkIntFromBuffer(const char* buff) {
// Make a network-byte-ordered integer from the fetched data
const int *network = reinterpret_cast<const int*>(buff);
// Convert to host (local) byte order and return
int host = ntohl(*network);
return host;
}
int PgColumn::GetInt(int row) {
if (IsNull(row) || row > Rows() || GetLength(row) != 4)
return 0;

return NetworkIntFromBuffer(PQgetvalue(m_Res, row, m_Col));
}

Quote:
Thanks a bunch! Now here's the old posting:

On Monday 27 October 2003 09:15, Tomasz Myrta wrote:
Dnia 2003-10-27 00:08, UÅytkownik creid napisaÅ:
Problem: Assigning a COUNT(*) result into an integer variable in my
C
program consistently fails except when I assign the same result to a
char
variable. I can only assume that the internal data type the COUNT
function uses is integer.

Can anyone help put me in the proper mindset so I may deal with this,
seemingly simple issue, to resolution.

I need the integer result to to help me satisfy a dynamic memory
requirement... COUNT(*) result will tell me how many rows of data I
need
to malloc and I cannot perform a math operation on a char variable.

All libpq results are strings.
some_int_value=atoi(PQgetvalue(...))

Not true anymore with protocol v3, which added the binary format. Text
format
is still the default.

Anyway why do you need count(*) ? When you retrieve your rows, you can
always check how many are them using PQntuples(...) and then malloc
your
memory tables.

Regards,
Tomasz Myrta

---------------------------(end of
broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly

--
David Helgason,
Business Development et al.,
Over the Edge I/S (http://otee.dk)
Direct line +45 2620 0663
Main line +45 3264 5049


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.