Re: How to send multiple SQL commands from Python?

Поиск
Список
Период
Сортировка
От Adrian Klaver
Тема Re: How to send multiple SQL commands from Python?
Дата
Msg-id 200910101227.39778.aklaver@comcast.net
обсуждение исходный текст
Ответ на How to send multiple SQL commands from Python?  (Kynn Jones <kynnjo@gmail.com>)
Ответы Re: How to send multiple SQL commands from Python?  (Adrian Klaver <aklaver@comcast.net>)
Re: How to send multiple SQL commands from Python?  (Rich Shepard <rshepard@appl-ecosys.com>)
Список pgsql-general
On Saturday 10 October 2009 12:09:29 pm Kynn Jones wrote:
> I am porting some code from Perl to Python; in the Perl original I use
> either DBI::do or a rickety home-built module to pass multiple SQL
> statements (as one single block of SQL) to the Pg server.  The typical
> usage is something like this:
> $dbh->do( <<EOSQL );
> ALTER TABLE $xn OWNER TO xdev;
> GRANT ALL ON TABLE $xn TO xdev;
>
> REVOKE ALL ON TABLE $xn FROM PUBLIC;
> GRANT SELECT ON TABLE $xn TO PUBLIC;
> EOSQL
>
>
> How can I do this sort of thing from Python?
>
> I've looked into the docs for psycopg2, but I can't find anything like the
> do command used above.  Did I overlook it?  If not, what can I use instead?
>
> I'm not wedded to psycopg2, in fact its lack of documentation worries me;
> if there's a better alternative that I can use from Python please let me
> know.
>
> TIA!
>
> kynn

One way
Using psycopg2
DSN = "dbname=? user=? port=? host=?"
con = psycopg2.connection(DSN)
cur = con.cursor()
cur.execute(statement1)
cur.execute(statement2)
....
con.commit()

Another way, not tested, is triple quote entire block above and pass it to
cur.execute.

--
Adrian Klaver
aklaver@comcast.net

В списке pgsql-general по дате отправления:

Предыдущее
От: Scott Ribe
Дата:
Сообщение: Re: Integer range?
Следующее
От: Christophe Pettus
Дата:
Сообщение: Re: Building PG 8.4.1 with ossp-uuid on Centos 5.3