Re: Re: PgBoolean

[email protected]
Newsgroups gmane.comp.python.db.pypgsql.user
Message-ID <[email protected]>
[email protected] 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....: [email protected]
|  /|      | 7436 Hartwell     | MSN.......: [email protected]
|-/-|----- | Dearborn, MI 48126|
|/  |LLIE  | (313) 582-1540    |
signature.asc (application/pgp-signature, 1.9 KB)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Content-Type: text/plain; charset=us-ascii

[email protected] 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....: [email protected]
|  /|      | 7436 Hartwell     | MSN.......: [email protected]
|-/-|----- | Dearborn, MI 48126|
|/  |LLIE  | (313) 582-1540    |


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.2 (UnixWare)
Comment: Exmh version 2.2 06/23/2000

iD8DBQE93HsYnmIkMXoVVdURAsOAAKDRszOHXbK/tfLmZ+MWWJk2Fc4f/QCfSNP2
Tc80HCRnzGm05C5AcnSW9ew=
=E2CW
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.