Postgres COPY Command with python 2.3 pg

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Lang

    #1

    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("inpu t|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)

    #2
    Re: Postgres COPY Command with python 2.3 pg

    Michael Lang wrote:[color=blue]
    > 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.[/color]

    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(D SN)
    db.autocommit = 1
    con = db.cursor()

    In my code.

    Comment

    • Michael Lang

      #3
      Re: Postgres COPY Command with python 2.3 pg

      On 2005-02-15, @(none) <""> wrote:[color=blue]
      > Michael Lang wrote:[color=green]
      >> 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.[/color]
      >
      > 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(D SN)
      > db.autocommit = 1
      > con = db.cursor()[/color]

      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.[color=blue][color=green][color=darkred]
      >>> import pg
      >>> db = pg.DB('bind9', '192.168.192.2' , 5432, None, None, 'named', None)
      >>> dir(db)[/color][/color][/color]
      ['_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 ...
      [color=blue]
      >
      > In my code.[/color]

      Comment

      • gry@ll.mit.edu

        #4
        Re: Postgres COPY Command with python 2.3 pg

        import pg
        db = pg.DB('bind9', '192.168.192.2' , 5432, None, None, 'named', None)
        db.query('creat e 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('commi t')

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

        Comment

        Working...