Re: simple example of copying data from a c/c++ array into postgres

From: "Haszlakiewicz, Eric" <EHASZLA(at)transunion(dot)com>
To: "Jeroen Vermeulen" <jtv(at)xs4all(dot)nl>, "Whit Armstrong" <armstrong(dot)whit(at)gmail(dot)com>
Cc: <pgsql-interfaces(at)postgresql(dot)org>
Subject: Re: simple example of copying data from a c/c++ array into postgres
Date: 2008-12-09 17:26:52
Message-ID: 9D29FD18CBD74A478CBA86E6EF6DBAD403080E18@CHI4EVS04.corp.transunion.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)postgresql(dot)org
>[mailto:pgsql-interfaces-owner(at)postgresql(dot)org] On Behalf Of
>Jeroen Vermeulen
>Sent: Tuesday, December 09, 2008 10:53 AM
>To: Whit Armstrong
>Cc: pgsql-interfaces(at)postgresql(dot)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(at)postgresql(dot)org)
>To make changes to your subscription:
>http://www.postgresql.org/mailpref/pgsql-interfaces
>

In response to

Browse pgsql-interfaces by date

  From Date Subject
Next Message Whit Armstrong 2008-12-09 17:36:35 Re: simple example of copying data from a c/c++ array into postgres
Previous Message Jeroen Vermeulen 2008-12-09 16:53:05 Re: simple example of copying data from a c/c++ array into postgres