473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.

Aug 3 '06 #1
7 2175

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

Aug 3 '06 #2

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

Aug 3 '06 #3

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.

Aug 3 '06 #4
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
Aug 3 '06 #5
"damacy" <we****@gmail.comwrote:
>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, ti**@probo.com
Providenza & Boekelheide, Inc.
Aug 4 '06 #6
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" <we****@gmail.comwrote:
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Aug 22 '06 #7
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" <we****@gmail.comwrote:
>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, ti**@probo.com
Providenza & Boekelheide, Inc.
Aug 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Zunbeltz Izaola | last post by:
Hi! I am planning a program and I need some advice about what tool to use. Basically my program will deal with a object A. A is a list like object with same attributtes and a list of objects...
28
by: dcrespo | last post by:
Hi all, How can I get a raised exception from other thread that is in an imported module? For example: --------------- programA.py ---------------
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
7
by: Michele Petrazzo | last post by:
Hi list, I have a strange error on my software on win 2k/xp and debian 3.1 with py 2.3.5 / 2.4.1 + twisted + wxpython: python, on a piece of code doesn't raise a KeyError on a dict (that don't...
8
by: Paul Rubin | last post by:
I'd like to suggest adding a builtin abstract class to Python called AsynchronousException, which would be a subclass of Exception. The only asynchronous exception I can think of right now is...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
2
by: Petr Jakes | last post by:
I am a little bit confused by all possibilities for exceptions handling in Python (probably because I am not skilled enough??) I did try to search trough this list and reading Python tutorial about...
3
by: yinglcs | last post by:
I read the document here about exception handling in python: http://www.diveintopython.org/file_handling/index.html Can you please tell me how can I catch all exception in python? like this in...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.