Re: code example for PQgetCopyData

From: "Bret" <bret_stern(at)machinemanagement(dot)com>
To: "'Dave Huber'" <DHuber(at)letourneautechnologies(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: code example for PQgetCopyData
Date: 2009-12-03 17:57:54
Message-ID: 000f01ca7442$24e63290$0d00a8c0@bjsworkstation
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

_____

From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Dave Huber
Sent: Thursday, December 03, 2009 9:18 AM
To: pgsql-general(at)postgresql(dot)org
Subject: [GENERAL] code example for PQgetCopyData

Does anybody have a snippet where they use PQgetCopyData? I must be calling
it wrong as it keep crashing my program. I've attached my code below. I am
writing this for a Code Interface Node in LabVIEW.

Thanks,

Dave

MgErr CINRun(LStrHandle conninfo, LStrHandle copystr, TD1Hdl resultValues) {

MgErr err = noErr;

PGconn *pConn;

PGresult* pResult = NULL;

char* szCopyStr = NULL;

char* errormsg = NULL;

char** buffer = NULL; // for retrieving the data

int nLen; // length of returned data

// connect to the database

char* szConnInfo = new char[LHStrLen(conninfo)+1];

LToCStr((*(conninfo)), (CStr)szConnInfo);

pConn = PQconnectdb(szConnInfo);

delete [] szConnInfo;

// check for errors connecting to database

if (PQstatus(pConn) != CONNECTION_OK)

{

DbgPrintf("Connection to database failed: %s",
PQerrorMessage(pConn));

}

else

{

// start the copy command

szCopyStr = new char[LHStrLen(copystr)+1];

LToCStr((*(copystr)), (CStr)szCopyStr);

pResult = PQexec(pConn, szCopyStr);

delete [] szCopyStr;

// get the data

int i = 0;

while (nLen = PQgetCopyData(pConn, buffer, false) > 0)

{

if (err = SetCINArraySize((UHandle)resultValues, 2, ++i))

goto out;

if (err = NumericArrayResize(uB, 1L,
(UHandle*)(&(*resultValues)->elt[i-1]), nLen-1))

goto out;

LStrLen(*(*resultValues)->elt[i-1]) = nLen-1;
// set the Labview String size

MoveBlock(*buffer, LStrBuf(*(*resultValues)->elt[i-1]),
nLen-1); // copy the data to a new string

PQfreemem(*buffer);
// free the memory from getCopy

}

(*resultValues)->dimSize = i;

out:

PQclear(pResult);

// see if there were errors

if (nLen == -2)

{

DbgPrintf("Copy Out failed: %s", PQerrorMessage(pConn));

}

}

// close the connection

PQfinish(pConn);

return err;

}


Where is it blowing up?

_____

This electronic mail message is intended exclusively for the individual(s)
or entity to which it is addressed. This message, together with any
attachment, is confidential and may contain privileged information. Any
unauthorized review, use, printing, retaining, copying, disclosure or
distribution is strictly prohibited. If you have received this message in
error, please immediately advise the sender by reply email message to the
sender and delete all copies of this message.
THIS E-MAIL IS NOT AN OFFER OR ACCEPTANCE: Notwithstanding the Uniform
Electronic Transactions Act or any other law of similar import, absent an
express statement to the contrary contained in this e-mail, neither this
e-mail nor any attachments are an offer or acceptance to enter into a
contract, and are not intended to bind the sender, LeTourneau Technologies,
Inc., or any of its subsidiaries, affiliates, or any other person or entity.
WARNING: Although the company has taken reasonable precautions to ensure no
viruses are present in this email, the company cannot accept responsibility
for any loss or damage arising from the use of this email or attachments.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Felt 2009-12-03 18:11:26 Re: DB terminating
Previous Message Israel Brewster 2009-12-03 17:33:25 Re: Build universal binary on Mac OS X 10.6?