exception handling; python program that interacts with postgresql db

D

damacy

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.
 
H

hiaips

damacy said:
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
 
D

damacy

hiaips said:
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.
 
T

Tim Roberts

damacy said:
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;" )
 
D

damacy

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?
 
D

Dennis Lee Bieber

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?
Do a commit; and try the "DROP" without any other operations ahead
of it.?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top