Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

exception handling; python program that interacts with postgresql db

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


wegein at gmail

Aug 2, 2006, 6:15 PM

Post #1 of 8 (159 views)
Permalink
exception handling; python program that interacts with postgresql db

hi, there. i have this question which might sound quite stupid to some
people, but here we go anyway.

i have written a python program which interacts with a postgresql
database. what it does is simply drops an existing database called
'mytempdb'.

the code looks like below;

link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
subprocess.PIPE, shell = True)
link.communicate(password)
link.wait()

where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
and
filename is the name of the file which contains a single SQL command
which is "drop database mytempdb".

the program works fine as long as a correct password is supplied,
however, i have a problem if the password is incorrect since this
exception is *not* handled within the scope of my program, instead,
what is does is showing some error messages in the prompt. so my
program, without knowing whether an errors has taken place or not, goes
on to execute the next task.

any clue? please let me know if you think the problem is not well
addressed. =)

thanks. have a nice one.

--
http://mail.python.org/mailman/listinfo/python-list


rosedb0 at gmail

Aug 2, 2006, 6:30 PM

Post #2 of 8 (153 views)
Permalink
Re: exception handling; python program that interacts with postgresql db [In reply to]

damacy wrote:
> hi, there. i have this question which might sound quite stupid to some
> people, but here we go anyway.
>
> i have written a python program which interacts with a postgresql
> database. what it does is simply drops an existing database called
> 'mytempdb'.
>
> the code looks like below;
>
> link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> subprocess.PIPE, shell = True)
> link.communicate(password)
> link.wait()
>
> where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> and
> filename is the name of the file which contains a single SQL command
> which is "drop database mytempdb".
>
> the program works fine as long as a correct password is supplied,
> however, i have a problem if the password is incorrect since this
> exception is *not* handled within the scope of my program, instead,
> what is does is showing some error messages in the prompt. so my
> program, without knowing whether an errors has taken place or not, goes
> on to execute the next task.
>
> any clue? please let me know if you think the problem is not well
> addressed. =)
>
> thanks. have a nice one.

Hi, damacy,

Maybe I'm not understanding your code 100%, but have you tried catching
the return value of the psql process that you're launching? Just a
thought...

--hiaips

--
http://mail.python.org/mailman/listinfo/python-list


rosedb0 at gmail

Aug 2, 2006, 6:36 PM

Post #3 of 8 (159 views)
Permalink
Re: exception handling; python program that interacts with postgresql db [In reply to]

Another option would be to use the psycopg module to connect to
postgres from within your Python code. See
http://www.initd.org/projects/psycopg1 for more information.

--hiaips

--
http://mail.python.org/mailman/listinfo/python-list


wegein at gmail

Aug 2, 2006, 8:09 PM

Post #4 of 8 (151 views)
Permalink
Re: exception handling; python program that interacts with postgresql db [In reply to]

hiaips wrote:
> damacy wrote:
> > hi, there. i have this question which might sound quite stupid to some
> > people, but here we go anyway.
> >
> > i have written a python program which interacts with a postgresql
> > database. what it does is simply drops an existing database called
> > 'mytempdb'.
> >
> > the code looks like below;
> >
> > link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> > subprocess.PIPE, shell = True)
> > link.communicate(password)
> > link.wait()
> >
> > where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> > and
> > filename is the name of the file which contains a single SQL command
> > which is "drop database mytempdb".
> >
> > the program works fine as long as a correct password is supplied,
> > however, i have a problem if the password is incorrect since this
> > exception is *not* handled within the scope of my program, instead,
> > what is does is showing some error messages in the prompt. so my
> > program, without knowing whether an errors has taken place or not, goes
> > on to execute the next task.
> >
> > any clue? please let me know if you think the problem is not well
> > addressed. =)
> >
> > thanks. have a nice one.
>
> Hi, damacy,
>
> Maybe I'm not understanding your code 100%, but have you tried catching
> the return value of the psql process that you're launching? Just a
> thought...
>
> --hiaips

hi, hiaips. thanks for your reply.

are you talking about a try-except block? yes, i used that in case the
psql process might throw an exception if there is any. but
unfortunately, it does not do so.

--
http://mail.python.org/mailman/listinfo/python-list


wegein at gmail

Aug 2, 2006, 8:09 PM

Post #5 of 8 (157 views)
Permalink
Re: exception handling; python program that interacts with postgresql db [In reply to]

yes, i'll have a read. thanks. =)


hiaips wrote:
> Another option would be to use the psycopg module to connect to
> postgres from within your Python code. See
> http://www.initd.org/projects/psycopg1 for more information.
>
> --hiaips

--
http://mail.python.org/mailman/listinfo/python-list


timr at probo

Aug 3, 2006, 11:10 PM

Post #6 of 8 (153 views)
Permalink
Re: exception handling; python program that interacts with postgresql db [In reply to]

"damacy" <wegein [at] gmail> wrote:

>hi, there. i have this question which might sound quite stupid to some
>people, but here we go anyway.
>
>i have written a python program which interacts with a postgresql
>database. what it does is simply drops an existing database called
>'mytempdb'.
>
>the code looks like below;
>
>link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
>subprocess.PIPE, shell = True)
>link.communicate(password)
>link.wait()
>
>where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
>and
>filename is the name of the file which contains a single SQL command
>which is "drop database mytempdb".

hiaips is right. The right way to do this is to use a Postgres module.
psycopg is my favorite, but there are several alternatives.

import psycopg
db = psycopg.connect(
"dbname=template1 user=postgres password=%s" % password )
c = db.cursor()
c.execute( "drop database mytempdb;" )
--
- Tim Roberts, timr [at] probo
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


wegein at gmail

Aug 21, 2006, 8:13 PM

Post #7 of 8 (151 views)
Permalink
Re: exception handling; python program that interacts with postgresql db [In reply to]

thanks. i started to use psycopg.

however, i have this error message and i don't quite get what it means.

it says "DROP DATABASE cannot run inside a transaction block".

does anyone have a clue?

Tim Roberts wrote:
> "damacy" <wegein [at] gmail> wrote:
>
> >hi, there. i have this question which might sound quite stupid to some
> >people, but here we go anyway.
> >
> >i have written a python program which interacts with a postgresql
> >database. what it does is simply drops an existing database called
> >'mytempdb'.
> >
> >the code looks like below;
> >
> >link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> >subprocess.PIPE, shell = True)
> >link.communicate(password)
> >link.wait()
> >
> >where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> >and
> >filename is the name of the file which contains a single SQL command
> >which is "drop database mytempdb".
>
> hiaips is right. The right way to do this is to use a Postgres module.
> psycopg is my favorite, but there are several alternatives.
>
> import psycopg
> db = psycopg.connect(
> "dbname=template1 user=postgres password=%s" % password )
> c = db.cursor()
> c.execute( "drop database mytempdb;" )
> --
> - Tim Roberts, timr [at] probo
> Providenza & Boekelheide, Inc.

--
http://mail.python.org/mailman/listinfo/python-list


wegein at gmail

Aug 21, 2006, 10:47 PM

Post #8 of 8 (153 views)
Permalink
Re: exception handling; python program that interacts with postgresql db [In reply to]

oh, fixed when i set isolation level to 0.
thanks anyway!

damacy wrote:
> thanks. i started to use psycopg.
>
> however, i have this error message and i don't quite get what it means.
>
> it says "DROP DATABASE cannot run inside a transaction block".
>
> does anyone have a clue?
>
> Tim Roberts wrote:
> > "damacy" <wegein [at] gmail> wrote:
> >
> > >hi, there. i have this question which might sound quite stupid to some
> > >people, but here we go anyway.
> > >
> > >i have written a python program which interacts with a postgresql
> > >database. what it does is simply drops an existing database called
> > >'mytempdb'.
> > >
> > >the code looks like below;
> > >
> > >link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> > >subprocess.PIPE, shell = True)
> > >link.communicate(password)
> > >link.wait()
> > >
> > >where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> > >and
> > >filename is the name of the file which contains a single SQL command
> > >which is "drop database mytempdb".
> >
> > hiaips is right. The right way to do this is to use a Postgres module.
> > psycopg is my favorite, but there are several alternatives.
> >
> > import psycopg
> > db = psycopg.connect(
> > "dbname=template1 user=postgres password=%s" % password )
> > c = db.cursor()
> > c.execute( "drop database mytempdb;" )
> > --
> > - Tim Roberts, timr [at] probo
> > Providenza & Boekelheide, Inc.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.