Re: executing a sql script

From: Adrian Klaver <aklaver(at)comcast(dot)net>
To: pgsql-general(at)postgresql(dot)org
Cc: johnf <jfabiani(at)yolo(dot)com>
Subject: Re: executing a sql script
Date: 2009-01-17 15:14:06
Message-ID: 200901170714.06283.aklaver@comcast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Friday 16 January 2009 10:09:06 pm johnf wrote:
> I'm using python and can execute standard "select,update,delete,functions".
> What I'd like to do is execute a sql script (a text file). But I don't
> know how?
> Some thing like:
> import psycopg2
> import psycopg2.extensions
> conn = psycopg2.connect("host=%s dbname=%s user =%s password
> =%s "
> %(self.pgSqlHostID.Value,self.pgSqlDatabaseID.Value,self.pgSqlUserID.Value,
>self.msSqlPasswordID.Value))
>
> conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
> tempCursor= conn.cursor()
> try:
> tempCursor.execute("run script %s " % FileNameScript)
> tempCursor.execute('commit')
> --
> John Fabiani

I don't know how complicated the script files are, but two methods come to mind.
The first is to use open() to read the file and parse the lines to the
execute() method. The second would be to use os.system() to do psql -d db -U
user -f FileNameScript.

--
Adrian Klaver
aklaver(at)comcast(dot)net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message johnf 2009-01-17 15:21:37 Re: executing a sql script
Previous Message Martijn van Oosterhout 2009-01-17 11:26:21 Re: Autovacuum daemon terminated by signal 11