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 1m3j8x-0007Z6-Ob for psycopg@arkaria.postgresql.org; Wed, 14 Jul 2021 17:58: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 1m3j8w-0000zy-O4 for psycopg@arkaria.postgresql.org; Wed, 14 Jul 2021 17:57:58 +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 1m3j8v-0000zq-Ts for psycopg@lists.postgresql.org; Wed, 14 Jul 2021 17:57:58 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3j8r-0004DJ-LY for psycopg@postgresql.org; Wed, 14 Jul 2021 17:57:57 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id 6466A3200708; Wed, 14 Jul 2021 13:57:49 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Wed, 14 Jul 2021 13:57:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=T 5R8u4PL7aDvs70EbJkw9r6z3pkIt6yP3GWkz6KneEc=; b=dqgfq5TVtcAj4qtAx Y/KnMULUmIydpArXV2TpnvEFzvDuwFutovTVDInZMVNjTrsLrRi/JaOKAz+naFlb cSxZlwqTafDlu2tXw6HV4/1+BFTGdXQk5/dbOHqktpIYEt0IXcdD5iKzShc3qf+z Vm74U+UR1BM/4/uzXc8FikCJMN04Z/tsUAa2KPCn98QJL3pyjCBNIzhqQxBpvsWX i5C8S0qKGdrBwvszzaq5OYKyg31SdnEDjYhvyCf2DNnMnDjNDZ2ulijhKAF05OvW O8XPYN/fMAbokHKDCmNqS1zm7envRrFtjPoPYfrovr23UmCm5PzKVHNdSZGM8+WB r4F4w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=T5R8u4PL7aDvs70EbJkw9r6z3pkIt6yP3GWkz6Kne Ec=; b=QYYq1Yq4pkYwOf6Ejd4dKOrVLh7SOEDb7CfUbCwz+LlRuuQkxiqXzPJl+ 0FzprRL3YQeaUUuCaxjrUPmMoc0JqB3uzMZxuA8vSZMbS1S68tB5Z9GM1p7Zspdt k6pmzth7BoR0TqRb0OhwRkxtUMvB8r9JwWCvUGzoX+cjJhiLhrdbLpZ3EnKU7CRe DYPSE4yaCIrKFIJE14sHU7aQAy77ziP+24gpbb7zme3rJm46WZ3pIGbr566p6qVZ aHwPx8Aa9jmm9hC9tGf6fxb/ZjuscFCh8wxzXIiZFK/HSvuWm2tgtvOHcaDBa23p BZ/oKRHTRWDoq6f65+jsNvuWc+Tqg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudekgdduudehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeeihedtleegudeuteeltdejgfelfffgkefhiedtfeeh jeehvdffvdevuedtkefgvdenucffohhmrghinhepphhshigtohhpghdrohhrghdpshhtrg gtkhhovhgvrhhflhhofidrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 14 Jul 2021 13:57:48 -0400 (EDT) Subject: Re: New user questions To: Rich Shepard , psycopg@postgresql.org References: <36d964b4-e04b-5fdf-9b21-d3bf80a3505d@aklaver.com> <9b477722-71af-abbb-8db7-1390a9906b07@aklaver.com> From: Adrian Klaver Message-ID: Date: Wed, 14 Jul 2021 10:57:47 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/14/21 10:35 AM, Rich Shepard wrote: > 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])) Where are c.nbr and a.* coming from? If they are supposed to be table qualified values this will not work. Also it is sql.Identifier. > > 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)) This will not work, if for no other reason that 'companies as c on c.org_nbr = p.org_nbr', etc are not identifiers and it is sql.Identifier. Also it would be simpler for this case just to create a quoted statement, something like: """SELECT fld_1, fld_2, fld_3 FROM some_table AS st JOIN another_table AS a ON st.id = a.a_id WHERE st.fld_1 = 'something' """ > >> 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 > > -- Adrian Klaver adrian.klaver@aklaver.com