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 1m3ing-0006sY-Nt for psycopg@arkaria.postgresql.org; Wed, 14 Jul 2021 17:36:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m3inf-00082i-MJ for psycopg@arkaria.postgresql.org; Wed, 14 Jul 2021 17:35:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3inf-00082a-Go for psycopg@lists.postgresql.org; Wed, 14 Jul 2021 17:35:59 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3inc-0007Ie-5Z for psycopg@postgresql.org; Wed, 14 Jul 2021 17:35:58 +0000 Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id AA2532A0106 for ; Wed, 14 Jul 2021 10:35:54 -0700 (PDT) Date: Wed, 14 Jul 2021 10:35:54 -0700 (PDT) From: Rich Shepard To: psycopg@postgresql.org Subject: Re: New user questions In-Reply-To: <9b477722-71af-abbb-8db7-1390a9906b07@aklaver.com> 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 Tue, 13 Jul 2021, Adrian Klaver wrote: > Then you want to take a look at this: > https://www.psycopg.org/docs/sql.html Adrian, Based on pages 1-2 of that doc I've re-written one each of an insert and select query. When they are correct I'll do the rest of the insert and select queries. Query 1: cur.execute( sql.SQL("insert into {} values (%s, %s, %s, %s, %s)" .format(sql.identifier('contacts')), [c.nbr, a.date, a.type, a.notes, a.next])) Query 2: query = sql.SQL("select {fields} from {tables}").format( fields = sql.SQL(',').join([ sql.identifier('p.lname'), sql.identifier('p.fname'), sql.identifier('p.loc_nbr'), sql.identifier('p.job_title'), sql.identifier('p.direct_phone'), sql.identifier('p.active'), sql.identifier('c.org_name'), sql.identifier('l.loc_nbr'), sql.identifier('l.loc_name'), sql.identifier('a.act_date'), sql.identifier('a.act_type'), sql.identifier('a.notes'), sql.identifier('a.next_contact'), ]) tables = sql.SQL(',').join([ sql.identifier('people as p'), sql.identifier('companies as c on c.org_nbr = p.org_nbr'), sql.identifier('locations as l on l.org_nbr = o.org_nbr and l.loc_nbr = p.loc_nbr'), sql.identifier('contacts as a on a.person_nbr = p.person_nbr'), ]) where p.lname = (%s) and p.fname = (%s) group by a.act_date order by a.act_date", (lname,fname)) > For an UPDATE example see my answer to this Stack Overflow question: > https://stackoverflow.com/questions/68321746/how-to-create-a-dynamic-update-query-in-sql-using-python/68323019#comment120800261_68323019 I'll read this again in close detail after I understand how to write insert and select queries. Regards, Rich