468,309 Members | 1,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,309 developers. It's quick & easy.

Postgres COPY Command with python 2.3 pg

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

Jul 18 '05 #1
3 2714
Michael Lang wrote:
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.
Jul 18 '05 #2
On 2005-02-15, @(none) <""> wrote:
Michael Lang wrote:
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()


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.
import pg
db = pg.DB('bind9', '192.168.192.2', 5432, None, None, 'named', None)
dir(db)

['_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 ...

In my code.

Jul 18 '05 #3
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 ...").

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bernardo Robelo | last post: by
6 posts views Thread by Prabu Subroto | last post: by
3 posts views Thread by Darkcamel | last post: by
5 posts views Thread by damacy | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.