Re: problems with returned data sets

Gerhard Haering <[email protected]> Thu, 4 Nov 2004 11:21:00 +0100
Newsgroups gmane.comp.python.db.pypgsql.user
Message-ID <[email protected]>
On Thu, Nov 04, 2004 at 05:55:11PM +1000, Timothy Smith wrote:
> ok i get that but it is still giving me problems. the error i get is
> libpq.OperationalError: ERROR:  column "groupmembership" does not exist
> 
> but it DOES exisit? there must be some syntax error i'm making. pyPgSQL 
> has bearly any real documentation so these errors are easy to make....
> 
> #Get groups which this user belongs to
>    cur.execute("SELECT UserGroup FROM UserMenuInfo WHERE UserName = 
> UserName")
>    Groups = cur.fetchone()
>    GroupMembership = Groups[0]
> 
> *snip*  
>   
>    #Get menu items which this user has access to
>    cur.execute("SELECT MenuName FROM MenuItems WHERE UserGroup = 
> GroupMembership")

"WHERE UserGroup = GroupMembership" means you join two columns in SQL. To
compare a column with a string, you use something like "WHERE UserGroup =
'Group1'", to compare it with a number "WHERE UserGroup = 42'.

Now, it appears you want to parametrize your query. That's what the additional
parameter of .execute() is for: to give it a sequence of query parameters:

cur.execute("SELECT Menuname FROM MenuItems WHERE UserGroup = %s", (GroupMembership,))

The placeholder in pyPgSQL is always %s.

-- Gerhard
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBigKMdIO4ozGCH14RArBSAJ49lkd+GX4EizX/5mOUauZ83ToeggCeK2+q
NVfe15SWJSf0GkK1LFE44i0=
=fSal
-----END PGP SIGNATURE-----