Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3mJK-0006BU-PR for psycopg@arkaria.postgresql.org; Wed, 14 Jul 2021 21:20:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m3mJJ-0004QF-Ha for psycopg@arkaria.postgresql.org; Wed, 14 Jul 2021 21:20:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3mJJ-0004Q7-Bx for psycopg@lists.postgresql.org; Wed, 14 Jul 2021 21:20:53 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3mJH-0005n3-CZ for psycopg@postgresql.org; Wed, 14 Jul 2021 21:20:53 +0000 Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 848FF2A0106 for ; Wed, 14 Jul 2021 14:20:49 -0700 (PDT) Date: Wed, 14 Jul 2021 14:20:49 -0700 (PDT) From: Rich Shepard To: psycopg@postgresql.org Subject: RE: New user questions In-Reply-To: Message-ID: References: <36d964b4-e04b-5fdf-9b21-d3bf80a3505d@aklaver.com> <9b477722-71af-abbb-8db7-1390a9906b07@aklaver.com> User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 14 Jul 2021, David Raymond wrote: > "The module contains objects and functions useful to generate SQL > dynamically, in a convenient and safe way." David, There are many examples in the doc and I'm just learning which is appropriate for each of my queries. > If there's nothing dynamic about the text of the query, then you don't > really need to go through all the trouble of using all those classes. > They're there in case you're getting unknown table names from a user, or > building a query on the fly, etc. If you know the query right now, you can > just put it into a text string, and call it good. No, nothing dynamic. > So your query2 might look something like this: That's closer to how the psql shell version looks. Revisions made. > Note that I don't think this query of yours is gonna work as you've got a > GROUP BY clause, and the SELECT list you have stuff that's not in the > GROUP BY, and is not an aggregate. Oops! Fixed it: query = """select p.person_nbr, p.lname,p.fname,p.loc_nbr,p.job_title,p.direct_phone,p.active, c.org_name, l.loc_nbr,l.loc_name, a.act_date,a.act_type,a.notes,a.next_contact from people as p, inner join companies as c on c.org_nbr = p.org_nbr, inner join locations as l on l.org_nbr = o.org_nbr and l.loc_nbr = p.loc_nbr, inner join contacts as a on a.person_nbr = p.person_nbr where p.lname = (%s) and p.fname = (%s) group by p.person_nbr order by a.act_date;""" cur.execute(query, (lname_value, fname_value)) Thanks, Rich