Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMDZd-0002TB-B5 for pgsql-general@arkaria.postgresql.org; Fri, 25 May 2018 14:20:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fMDZb-0005dR-Tp for pgsql-general@arkaria.postgresql.org; Fri, 25 May 2018 14:20:03 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMDZb-0005dA-Hs for pgsql-general@lists.postgresql.org; Fri, 25 May 2018 14:20:03 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMDZW-0007cs-Mu for pgsql-general@lists.postgresql.org; Fri, 25 May 2018 14:20:02 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 2489F2265F; Fri, 25 May 2018 10:19:57 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 25 May 2018 10:19:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=w18JoGnT3A8urhxCGR8e/vCNQ5zPn hbSfgojQg4t1dI=; b=bEHSf4PX0o4oSzjp0dT4xhNJIuAXf//Rri7NhJCyfNPzm COrBDYtykLIpawHCHIGl7Ec7Hxen7ja2w4tWNkvm/HNRWUbGT2tzmc08Ae4yZAkx ZQK1skJ+sP03ldONxbR1AxsWbDxdfnJzzAr+ZFpfsf7p3+s4qYWP1aWa9jnzENZN 6/Jptldy43gB3r+Slm+P6bBNv5LNIPvXbN9GgxOe4sgDu9NOAHOuhXdpM09tGa/D gbIB4+IzrLZmkZ8wzHLjF4yuLPXnsk4aqzIiRDE9wBFDT6UCkyBGuGTDiy0+jEAt PjSTklu5WrNMWfDztkb6lNuay1JS/rrpYAzW8Y1fA== 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-sender:x-me-sender:x-sasl-enc; s=fm2; bh=w18JoG nT3A8urhxCGR8e/vCNQ5zPnhbSfgojQg4t1dI=; b=VwZlg/pOSPJDBcLj0dVJmd 8+mFgWwshMg1f8ikwE9WnKzg1sTm0udAIXaYLF/RNCMrZD8drEGuDPo/TvHQkonR qoWDG/y0qiFf+ImI83uYvDulccNoM5lEHzl1AVo6LyaqHL8bNxsyQpP4JjOFC13z urClTuOVdagdiXQCqn2DbuXEflCIzL/GJ3njziIhLP0Hf8xTXaLQpiS/qkDqWkiS 3ux/7zrkPESFvqU2uUB8hAYfR7fUXhu4aNImBvsrRQMaUo9FY20Oh+Q8nfB29+o1 O3CXOjCrycM+LIRMR65gMzai0R1SGP8n7jkA7EAKQzTx4oELSoCl+tlRjbtwMvAQ == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id 72448E46C2; Fri, 25 May 2018 10:19:56 -0400 (EDT) Subject: Re: case statement within insert To: tango ward , "pgsql-generallists.postgresql.org" References: <626850f0-b6f8-3fdd-eaf0-6fcbff006737@aklaver.com> From: Adrian Klaver Message-ID: <9730aa5a-a2ac-4f1a-618c-50f377d2ab1a@aklaver.com> Date: Fri, 25 May 2018 07:19:55 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <626850f0-b6f8-3fdd-eaf0-6fcbff006737@aklaver.com> 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: Precedence: bulk On 05/25/2018 07:05 AM, Adrian Klaver wrote: > On 05/25/2018 06:52 AM, Adrian Klaver wrote: >> On 05/25/2018 02:04 AM, tango ward wrote: >>> >>> >>> I want to insert data from mysql into a table in postgresql. I want >>> to check when the subjectcode contains PE or NSTP so I can assign >>> True or False to another column in destination DB. >>> >>> >>> # Source data: >>> >>>   # Source data: MySQL >>>     curr_msql.execute(''' SELECT code, subjectname >>>                            FROM test_subj ''') >>> >>> # Destination >>> for row in curr_msql: >>>              curr_psql.execute(''' INSERT INTO subs ( >>>                                                      created, modified, >>>                                                      subjcode, subjname, >>>                                                      is_pe_or_nstp) >>> >>>                                    VALUES (current_timestamp, >>> current_timestamp, >>>                                            %s, %s, >>>                                            CASE >>>          WHEN code like '%%PE%%' or code like '%%NSTP%%' >> >> Shouldn't the above be?: >> >> subjcode like '%%PE%%' or subjcode like '%%NSTP%%' > > Turns out that won't work as you cannot refer to a column in the CASE: > > cur.execute("insert into  cell_per(category, cell_per, season, > plant_type, short_category)  values('herb test', 1, 'annual', CASE WHEN > category like '%%herb%%' THEN 'herb' ELSE 'flower' END, 'ht' )") > > ProgrammingError: column "category" does not exist > LINE 1: ...gory)  values('herb test', 1, 'annual', CASE WHEN category l... >                                                              ^ > HINT:  There is a column named "category" in table "cell_per", but it > cannot be referenced from this part of the query. > > > This works: > > cur.execute("insert into  cell_per(category, cell_per, season, > plant_type, short_category)  values(%s, 1, 'annual', CASE WHEN %s like > '%%herb%%' THEN 'herb' ELSE 'flower' END, 'ht' )", ('herb test', 'herb > test')) > > > So change code to row['code']? Insufficient caffeine. ... WHEN %s like '%%PE%%' or %s like '%%NSTP%%' ... , (row['code'], row['subjectname'], row['code'], row['code']) FYI this is why I like the named parameters then the above could be shortened to: {'code': row['code'], 'subjectname': row['subjectname']} and you get clearer query code: VALUES (current_timestamp, current_timestamp, %(code)s, %(subjectname)s, CASE WHEN %(code)s like '%%PE%%' or %(code)s like '%%NSTP%%' THEN True ELSE False END) > > > >> >>>          THEN True >>>          ELSE False >>>      END) ''', (row['code'], row['subjectname'])) >>> >>> I am getting TypeError: not all arguments converted during string >>> formatting. >>> >>> Any advice pls? >>> >>> >>> >>> >>> >>> >> >> > > -- Adrian Klaver adrian.klaver@aklaver.com