Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 3rd, 2006, 02:05 AM
damacy
Guest
 
Posts: n/a
Default 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.

  #2  
Old August 3rd, 2006, 02:25 AM
hiaips
Guest
 
Posts: n/a
Default Re: exception handling; python program that interacts with postgresql db


damacy wrote:
Quote:
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

  #3  
Old August 3rd, 2006, 02:35 AM
hiaips
Guest
 
Posts: n/a
Default Re: exception handling; python program that interacts with postgresql db


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

  #4  
Old August 3rd, 2006, 04:05 AM
damacy
Guest
 
Posts: n/a
Default Re: exception handling; python program that interacts with postgresql db


hiaips wrote:
Quote:
damacy wrote:
Quote:
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.

  #5  
Old August 3rd, 2006, 04:05 AM
damacy
Guest
 
Posts: n/a
Default Re: exception handling; python program that interacts with postgresql db

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


hiaips wrote:
Quote:
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
  #6  
Old August 4th, 2006, 07:05 AM
Tim Roberts
Guest
 
Posts: n/a
Default Re: exception handling; python program that interacts with postgresql db

"damacy" <wegein@gmail.comwrote:
Quote:
>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@probo.com
Providenza & Boekelheide, Inc.
  #7  
Old August 22nd, 2006, 04:05 AM
damacy
Guest
 
Posts: n/a
Default Re: exception handling; python program that interacts with postgresql db

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:
Quote:
"damacy" <wegein@gmail.comwrote:
>
Quote:
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@probo.com
Providenza & Boekelheide, Inc.
  #8  
Old August 22nd, 2006, 06:35 AM
damacy
Guest
 
Posts: n/a
Default Re: exception handling; python program that interacts with postgresql db

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

damacy wrote:
Quote:
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:
Quote:
"damacy" <wegein@gmail.comwrote:
Quote:
>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@probo.com
Providenza & Boekelheide, Inc.
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles