473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 5234
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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,...

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.