473,503 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PQSQL - binary data question

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:
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 ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
2 5237
On 29. okt 2004, at 14:09, Bastian Voigt wrote:
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));
}
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 ma*******@postgresql.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

Nov 23 '05 #2
On 29. okt 2004, at 14:09, Bastian Voigt wrote:
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));
}
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 ma*******@postgresql.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

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15648
by: kee | last post by:
Hi All, I am trying to write binary data to a file, which is bmp image: Open "d:\temp\test001.bmp" For Binary Access Write As #1 Put #1, 1, strImage Close #1 *** strImage contains binary...
1
8734
by: Niko Korhonen | last post by:
I'm currently in the process of programming a multimedia tagging library in standard C++. However, I've stumbled across one or two unclear issues while working with the library. First of all, is...
5
2177
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
8
25332
by: Jerry | last post by:
I have an off-the-shelf app that uses an Access database as its backend. One of the tables contains a field with an "OLE Object" datatype. I'm writing some reports against this database, and I...
4
3666
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
0
1236
by: Bastian Voigt | last post by:
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...
15
9767
by: mleaver | last post by:
I want to open a second window and display a binary image that is returned from a java program via XMLRPC. The data returned is a binary encoded base64 png file. If I write the data out to a file...
15
2372
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
3
4809
by: Freddy Coal | last post by:
Hi, I would like append strings to a binary file, but I don´t understand how make that. I try with: FileOpen(1, Folder_Trabajo & "\Toma_Trazas.FC", OpenMode.Append, OpenAccess.Write,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.