473,659 Members | 2,886 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*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
2 5258
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::SendPre pared(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 (! PQsendQueryPrep ared(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 NetworkIntFromB uffer(const char* buff) {
// Make a network-byte-ordered integer from the fetched data
const int *network = reinterpret_cas t<const int*>(buff);
// Convert to host (local) byte order and return
int host = ntohl(*network) ;
return host;
}
int PgColumn::GetIn t(int row) {
if (IsNull(row) || row > Rows() || GetLength(row) != 4)
return 0;

return NetworkIntFromB uffer(PQgetvalu e(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*******@postg resql.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::SendPre pared(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 (! PQsendQueryPrep ared(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 NetworkIntFromB uffer(const char* buff) {
// Make a network-byte-ordered integer from the fetched data
const int *network = reinterpret_cas t<const int*>(buff);
// Convert to host (local) byte order and return
int host = ntohl(*network) ;
return host;
}
int PgColumn::GetIn t(int row) {
if (IsNull(row) || row > Rows() || GetLength(row) != 4)
return 0;

return NetworkIntFromB uffer(PQgetvalu e(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*******@postg resql.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
15664
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 data
1
8753
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 it safe to store binary data in std::string? This question rose from my implementation with APEv2 tags. An APEv2 tag's field value can contain either UTF encoded text or binary data. I've decided to use std::string to represent the field value....
5
2209
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 numbers into 16 bit binary form, so that I can eventually alter the LSB for dithering purposes. Obviously I can create my own function that will do this, but I was wondering if there was some quick process that I could call up from a library that...
8
25381
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 believe this field contains data I need. When I view the table in datasheet view, all I can see in this field is the string "Long binary data". So, I've got the problem of needing to extract data from this field, but I don't know what format...
4
3684
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 following code that compiles OK. It runs and shows the requested output. However, after execution, it pops one of those windows to send error reports online to the porgram creator. I have managed to find where the error is but can't see what's wrong....
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 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:
15
9793
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 on my server, I can display it using the following javascript: var windowHandle = window.open('about:blank','windowName','width=250,height=250'); windowHandle.document.write('<img name="myImage" src="images/test.png">');
15
2396
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 two int32's as its data members, the binary file of that class must have a size of 8 bytes (i.e. only contains class data members, not methods etc.). Is serialization the answer to the above problem? If I understand correctly, the reason that...
3
4813
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, OpenShare.Default) FilePut(1, "My string") '<****** ERROR IN THIS LINE ****** FileClose(1)
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2750
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 we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.