From: <Bil...@mu...> - 2002-11-21 06:20:25
|
vze...@ve... wrote: > Thanks for the python tips. I guess it shows that I am new to the language :- > ) > > Unfortunately, I still can't seem to get this right. I tried this: > > if retval: > print "YES" > else: > print "NO" > > When executed, this prints out "YES" when the statement "print retval" prints > either > > [<PgBoolean instance at 0x401c890c: Value: True>] > > - or - > > [<PgBoolean instance at 0x401c8900: Value: False>] > > What am I doing? The problem is that you are testing the PgResultSet object, not the PgBoolean. The PgResultSet will always test true unless it's empty. Try doing the following: if retval[0]: print "Yes" else: print "No" Here is an example: >>> c = PgSQL.connect() >>> cu = c.cursor() >>> cu.execute("select 'f'::bool as myBool") >>> rs = cu.fetchone() >>> type(rs) <type 'instance'> >>> rs.__class__ <class pyPgSQL.PgSQL.PgResultSetConcreteClass at 805acdc> >>> >>> rs [<PgBoolean instance at bfd7e110: Value: False>] >>> myBool = rs['myBool'] # or rs.myBool or rs[0] >>> myBool f >>> print myBool f >>> repr(myBool) '<PgBoolean instance at bfd7e110: Value: False>' >>> if myBool: ... print "Yes" ... else: ... print "No" ... No >>> -- ____ | Billy G. Allie | Domain....: Bil...@mu... | /| | 7436 Hartwell | MSN.......: B_G...@em... |-/-|----- | Dearborn, MI 48126| |/ |LLIE | (313) 582-1540 | |