Skip site navigation (1) Skip section navigation (2)

Re: Performing COPY Command

From: "Rajan Bhide" <rbhide(at)nulinkinc(dot)com>
To: "Michael Glaesemann" <grzm(at)myrealbox(dot)com>,<pgsql-novice(at)postgresql(dot)org>
Subject: Re: Performing COPY Command
Date: 2004-02-10 14:15:13
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice

Can u provide me the 'C' code snippet to execute it (with libpq).(Specially to PIPE stdin to psql server) 
I tried with following but no success:

void performInsert()
     char        query_string[2048];     /* holds constructed SQL query */
     PGconn     *conn;                                   /* holds database connection */
     PGresult   *res;                                    /* holds query result */
     /* connect to the database */
     conn = PQconnectdb(DB_CONN_PARAM_STR);
     /* connect to the database */

    if (PQstatus(conn) == CONNECTION_BAD)               /* did the connection fail? */
      fprintf(stderr, "Connection to database failed.\n");
      fprintf(stderr, "%s\n",PQerrorMessage(conn));
      fprintf(stderr, "Connection successful\n");

    sprintf(query_string, "COPY testtable FROM stdin DELIMITERS ','");
    fprintf(stderr,"QueryStr : %s\n",query_string);
    res = PQexec(conn, query_string);                   /* send the query */
    if (atoi(PQcmdTuples(res)) == 0)
      fprintf(stderr, "Insert failed : %s\n",PQerrorMessage(conn));

    PQclear(res);                                       /* free result */
    PQfinish(conn);                                     /* disconnect from the database */

Table structure:
Psql=>\d testtable
Column |  Type   |                    Modifiers
 seqno  | integer |
 data   | bytea   |

I could succeed fetching the data from file,
but my code fails if the file contains non-printable characters saying "Bad input string for type bytea".

My contents from the file are:


Basically its the hexsequence from 0X00-0XFF.

Rajan Bhide

-----Original Message-----
From: Michael Glaesemann [mailto:grzm(at)myrealbox(dot)com] 
Sent: Tuesday, February 10, 2004 2:37 PM
To: Rajan Bhide
Subject: Re: [NOVICE] Performing COPY Command

Hi Rajan

On Feb 10, 2004, at 2:41 PM, Rajan Bhide wrote:

> How to perform COPY from memory instead of files?
> Any examples/pointers would be greatly appreciated.

I believe COPY works from STDIN if no file is specified, so if you can 
pipe the data to STDIN, you should be golden. And the docs might help: <>

Does this help?

Michael Glaesemann
grzm myrealbox com

pgsql-novice by date

Next:From: Nabil SayeghDate: 2004-02-10 14:44:27
Subject: psql scroll horizontal
Previous:From: Nabil SayeghDate: 2004-02-10 13:52:06
Subject: Re: function that returns dynamicly created table

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group