Re: insert tuples through copy

"Billy G. Allie" <[email protected]>
Newsgroups gmane.comp.python.db.pypgsql.user
Organization michigan!/usr/group
Message-ID <[email protected]>
Gaston Droguett wrote:

>Hello:
>Im trying to insert tuples through the copy sql like
>this:
>
>curs.execute('copy tablename from stdin;')
>curs.execute('2\t3\4\n')
>
>I get an error:
>    self.res = self.conn.conn.query(_qstr)
>libpq.ProgrammingError: COPY state must be terminated
>first
>
>I can't find the way to send the tuples string.
>
>In C you can do the following:
>
>PQexec(conn,"copy foo from stdin");
>PQputline(conn,"3\t4\n");
>PQputline(conn,"\\.\n";
>PQendcopy(conn);
>
>
>How can this be done with PgSQL??
>  
>
The PQputline and PQendcopy are exposed in the libpq PgConnection object 
as putline and endcopy.
The underlying libpq.PgConnection object is exposed as curs.conn.conn.
What you want to accomplish can be done as:

curs.execute('copy tablename from stdin')
curs.conn.conn.putline('2\t3\tr\n')
...
curs.conn.conn.endcopy()

In general, if there is something you want to do that is not supported 
by the DB-API 2.0 specification that PgSQL follows, but you would be 
able to do with th e libpq C API, then you can do it using the libpq 
module that PgSQL is built upon.  The libpq module exposes (most of) the 
C API available in the libpq library.

I hope this helps.

-- 
___________________________________________________________________________
____       | Billy G. Allie    | Domain....: [email protected]
|  /|      | 7436 Hartwell     | MSN.......: [email protected]
|-/-|----- | Dearborn, MI 48126|
|/  |LLIE  | (313) 582-1540    |




-------------------------------------------------------
Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
The only event dedicated to issues related to Linux enterprise solutions
www.enterpriselinuxforum.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.