Re: simple example of copying data from a c/c++ array into postgres - Mailing list pgsql-interfaces

From Haszlakiewicz, Eric
Subject Re: simple example of copying data from a c/c++ array into postgres
Date
Msg-id 9D29FD18CBD74A478CBA86E6EF6DBAD403080E18@CHI4EVS04.corp.transunion.com
Whole thread Raw
In response to Re: simple example of copying data from a c/c++ array into postgres  (Jeroen Vermeulen <jtv@xs4all.nl>)
List pgsql-interfaces
If you still want to do things as a straight insert, here's the key
lines from a test program I put together, minus all the error handling,
freeing of PGresult objects, etc...  I haven't actually tried inserting
floating point values, but I'm guessing it's similar to what is below.
Best way to tell is to try it, and see what ends up in the database.

PGresult *prep_result = PQprepare(conn, "exec_stmt", sql, 0, NULL);
#ifdef intparam
//    Oid paramTypes[1] = { INT4OID }; // from pg_type.h, needed if you
use PQexecParams
// you can't actually include pg_type.h easily because it has all sorts
of dependencies on other
// internal header files.  I ended writing a short script that
preprocesses that file and pulls
// out just the OID defines.   int fff = atoi(argv[1]);   fff = htonl(fff);   const char *paramValues[] = { (char
*)&fff};   int paramFormats[1] = { 1 }; // binary format   int paramLengths[1] = { sizeof(int) }; 
#else   date dfff;   dfff = PGTYPESdate_from_asc(datebuf, NULL);   // datebuf contains a YYYY-MM-DD format string
//    Oid paramTypes[1] = { DATEOID };   dfff = htonl(dfff);   const char *paramValues[] = { (char *)&dfff };   int
paramFormats[1]= { 1 }; // binary format   int paramLengths[1] = { sizeof(dfff) }; 
#endif
PGresult *exec_result = PQexecPrepared(conn, "exec_stmt", 1,
paramValues, paramLengths, paramFormats, 0);

>-----Original Message-----
>From: pgsql-interfaces-owner@postgresql.org
>[mailto:pgsql-interfaces-owner@postgresql.org] On Behalf Of
>Jeroen Vermeulen
>Sent: Tuesday, December 09, 2008 10:53 AM
>To: Whit Armstrong
>Cc: pgsql-interfaces@postgresql.org
>Subject: Re: [INTERFACES] simple example of copying data from
>a c/c++ array into postgres
>
>Whit Armstrong wrote:
>> would someone mind showing me an example of making an insert
>from binary
>> data to postgres?
>
>Not an example, but have a look at the COPY command:
>
>http://www.postgresql.org/docs/8.3/interactive/sql-copy.html
>
>COPY FROM stdin lets you insert data in bulk, without having
>to issue a
>new INSERT for every row.  There are some handy libpq functions for
>feeding data into this mechanism:
>
>http://www.postgresql.org/docs/8.3/interactive/libpq-copy.html
>
>The "binary" part of what you're asking for is also possible, but
>probably doesn't buy you all that much.  Chances are you'd need to do
>some conversions anyway, and it introduces all sorts of
>maintenance risk
>for an optimization that's not likely to matter as much as
>disk flushes,
>network transfers etc.
>
>
>Jeroen
>
>--
>Sent via pgsql-interfaces mailing list
>(pgsql-interfaces@postgresql.org)
>To make changes to your subscription:
>http://www.postgresql.org/mailpref/pgsql-interfaces
>


pgsql-interfaces by date:

Previous
From: Jeroen Vermeulen
Date:
Subject: Re: simple example of copying data from a c/c++ array into postgres
Next
From: "Whit Armstrong"
Date:
Subject: Re: simple example of copying data from a c/c++ array into postgres