[PyGreSQL] DB API-2.0 + copy from stdin

Christopher Sean Hilton chris at vindaloo.com
Tue Sep 23 13:48:10 EDT 2008


I know that this has been discussed here before. In the near future  
I'm going to
need to execute a "copy table from stdin" function to load some data.  
I've seen the posts for how to do this and it looks pretty simple but  
it's always done from a classic connection. I'm using a DBI connection  
and I'm loading into a temporary table. I am testing a module that  
will do something like this:

1) Extend the python pg.DB class to accept an existing connection  
returned by the _pg.connect() function.

2) Extend the pgdb.pgdbCnx class adding a method to return an instance  
of the the "classic" connection.

3) Extend the pgdb.connect() function to return an instance of the  
above class.

That way one could do:

import newpgdb

myDb = newpgdb.connect(...)

query = "CREATE TEMPORARY TABLE foo(foo_text text)"

c = myDb.cursor()
c.execute(query)

classicDb = myDb.get_classic_db()
classicDb.query("COPY foo from stdin")
for l in sys.stdin:
     classicDb.putline(l)
classicDb.putline('\\.\n')
classicDb.endcopy()

del classicDb

query = "INSERT INTO bar(bar_text) SELECT foo_text FROM foo"
c.execute(query)
myDb.commit()
myDb.close()

As I look at this I realize that I could accomplish all of this with a  
few patches to the stock pgdb module. Is this the right approach?

-- Chris

Chris Hilton                       tildeChris -- http://myblog.vindaloo.com
                                         email -- chris/at/vindaloo/ 
dot/com
.~ 
~ 
.--.~ 
~.--.~~.--.~~.--.~~.--.~~.--.~~.--.~~.--.~~.--.~~.--.~~.--.~~.--.~~.
                          "I'm on the outside looking inside, What do  
I see?
                                Much confusion, disillution, all  
around me."
                                          -- Ian McDonald / Peter  
Sinfield



More information about the PyGreSQL mailing list