Postgres COPY Command with python 2.3 pg

M

Michael Lang

Hi to all,

can some one point me to the correct way, how to use PostgreSQLs "COPY" feature
from within python ?

What i want to do is:

connect
start transaction
drop current tablecontens
copy new content from STDIN # file need more privileged user rights
commit transaction

using psql it works fine, but i dont know how to get it working in python.
Ive already made the calls but the changes never apper, and no error.
my suggestion was, that "db.query("input|input|input") doesnt work as STDIN for
the Database, and i tryed db.putline(....) but no success.

Kind regards for any help
Michael Lang
 
@

@(none)

Michael said:
using psql it works fine, but i dont know how to get it working in python.
Ive already made the calls but the changes never apper, and no error.

Which Postgres module are you using? I had the exct same problem when I
first started using pyPgSQL, until I figured out that I needed to do:

db = PgSQL.connect(DSN)
db.autocommit = 1
con = db.cursor()

In my code.
 
M

Michael Lang

Which Postgres module are you using? I had the exct same problem when I
first started using pyPgSQL, until I figured out that I needed to do:

db = PgSQL.connect(DSN)
db.autocommit = 1
con = db.cursor()

Hi,

im using PostgreSQL RPM from Fedora Core3
$ rpm -q postgresql-python
postgresql-python-7.4.6-1.FC3.2

code looks like
Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.['_DB__args', '_DB__attnames', '_DB__pkeys', '__doc__', '__init__', '__module__', '_do_debug', 'clear', 'close', 'db', 'debug', 'delete', 'endcopy', 'fileno', 'get', 'get_attnames', 'get_databases', 'get_tables', 'getline', 'getlo', 'getnotify', 'insert', 'inserttable', 'locreate', 'loimport', 'pkey', 'putline', 'query', 'reopen', 'reset', 'source', 'transaction', 'update']

so theres no cursor i could try like your code does ...
thanks for your response ...
 
G

gry

import pg
db = pg.DB('bind9', '192.168.192.2', 5432, None, None, 'named', None)
db.query('create temp table fffz(i int,t text)')
db.query('copy fffz from stdin')
db.putline("3\t'the'")
db.putline("4\t'rain'")
db.endcopy()
db.query('commit')

Note that multiple columns must be separated by tabs ('\t') (unless you
specify "copy mytable with delimiter ...").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,120
Members
47,741
Latest member
WilliamsFo

Latest Threads

Top