python + postgres psql + os.popen

Laszlo Nagy gandalf at designaproduct.biz
Thu Jun 22 09:11:36 CEST 2006


damacy írta:
> hello, everyone.
>
> i am trying to write a program which executes SQL commands stored in
> .sql files.
>
> i wrote a function called psql() whose contents look like the
> following.
>
> ...
> os.popen(command)
> file = os.popen(command, 'w')
> file.write(password)
> file.close()
> ...
>
> where command looks like
> psql -h [host] -d [dbname] -U [username] -W -f "[filename]"
>
> this works well. however, it does not show me any warning nor error
> messages if there is one. for example, i am trying to create a table
> which already exists in the database, it should show me a warning/error
> message saying there already is one present in the database, or
> something like that.
>
> can anyone help me?
>   
You can put this in the beginning of your SQL file:

\set ON_ERROR_STOP

If you also want to know what command caused the error:

\set ECHO all

You can also use a library written for Python. For example, psycopg 

http://initd.org/projects/psycopg1

Best,

    Laszlo





More information about the Python-list mailing list