Re: executing a sql script

From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: johnf <jfabiani(at)yolo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: executing a sql script
Date: 2009-01-17 17:41:31
Message-ID: 1232214091.7544.1.camel@jd-laptop.pragmaticzealot.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 2009-01-16 at 22:09 -0800, 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?

You need to open the text file and pass it as an argument:

try:
file = "%s/%s" % (str(sqlpath),str("myfile.sql"))
procedures = open(file,'r').read()
dbcur.execute(procedures)
except psycopg2.DatabaseError, e:
print
print "EXCEPTION: procedures :%s" % str(e)
print
exit(1)

> 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
>
--
PostgreSQL - XMPP: jdrake(at)jabber(dot)postgresql(dot)org
Consulting, Development, Support, Training
503-667-4564 - http://www.commandprompt.com/
The PostgreSQL Company, serving since 1997

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gregory Stark 2009-01-17 17:55:20 Re: MemoryContextSwitchTo (Re: [GENERAL] Autovacuum daemon terminated by signal 11)
Previous Message Alvaro Herrera 2009-01-17 17:15:46 Re: MemoryContextSwitchTo (Re: [GENERAL] Autovacuum daemon terminated by signal 11)