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 1fNEYQ-0003HE-Gt for pgsql-general@arkaria.postgresql.org; Mon, 28 May 2018 09:35:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fNEYP-00061S-5f for pgsql-general@arkaria.postgresql.org; Mon, 28 May 2018 09:35:01 +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 1fNEYO-00061J-QQ for pgsql-general@lists.postgresql.org; Mon, 28 May 2018 09:35:00 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fNEYK-0001Pl-QZ for pgsql-general@lists.postgresql.org; Mon, 28 May 2018 09:34:59 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 3E49F1AD0; Mon, 28 May 2018 11:34:51 +0200 (CEST) Date: Mon, 28 May 2018 11:34:51 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: case statement within insert Message-ID: <20180528093451.7qxudqsoncbexcvu@hjp.at> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="taouumsq7jejofry" Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --taouumsq7jejofry Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2018-05-25 17:04:25 +0800, tango ward wrote: >=20 >=20 > I want to insert data from mysql into a table in postgresql. I want to ch= eck > when the subjectcode contains PE or NSTP so I can assign True or False to > another column in destination DB. >=20 >=20 > # Source data: >=20 > =A0# Source data: MySQL > =A0=A0 curr_msql.execute(''' SELECT code, subjectname > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 FROM test_subj ''') >=20 > # Destination > for row in curr_msql: > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 curr_psql.execute(''' INSERT INTO subs ( > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 created, modified, > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 subjcode, subjname, > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 is_pe_or_nstp) >=20 > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 VALUES (current_timestamp, current_timestamp, > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 %s, %s, > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 CASE > =A0=A0=A0=A0=A0=A0=A0 WHEN code like '%%PE%%' or code like '%%NSTP%%' > =A0=A0=A0=A0=A0=A0=A0 THEN True > =A0=A0=A0=A0=A0=A0=A0 ELSE False > =A0=A0=A0 END) ''', (row['code'], row['subjectname'])) Others have already explained why that doesn't work and how you can fix it. But since you are inserting one row at a time in a Python loop, I don't see any advantage in writing the condition in SQL. Do it in Python instead: for row in curr_msql: is_pe_or_nstp =3D 'PE' in row['code'] or 'NSTP' in row['code'] curr_psql.execute(''' INSERT INTO subs ( created, modified, subjcode, subjname, is_pe_or_nstp) VALUES (current_timestamp, current_timestamp, %s, %s, %s) ''', (row['code'], row['subjectname'], is_pe_or_nstp,)) A bit more readable, IMHO. Alternatively, import the complete table *unchanged* from MySQL (you can use copy_from() for this which is much faster than individual inserts), and then convert it with a single SQL statement. hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --taouumsq7jejofry Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAlsLzTYACgkQ8g5IURL+ KF050BAAgY54y6QQjnqNf1xQY7nP72N0G/9hz5x4PdJUkoOKTVXTKZE2wCcrykMh yj6lUZnNTE/G6sfTkijuZ72S0bLvbEFHVFFG8b7KJ8OXbmx665UEKotbLo/xLeku k87SK48wmlHMoAQc1nIhnT/Ema9bB2sMZJgmXnWgszsGyo9/26kbLQ+Jg6Wl/4oZ 0l9eeUzsEnqa8azhzln6coYFf4bT0UbDSufZ3/707FBTSfCDs8Qbm4IdOyLOq/ly 1nVrrzrsOYtAnDBZwBO7rKGeiWI/lflPTGCUuViF9KjIGEOe/wi0RMkpwCI+4iWD XNFVCZWopjhLRD21dS/b9yZZd9hWwAyXwKZpUazUqWHma+/dhBzw0RJOyo/WCFAx uDZHICpbha4vuWOkc1FYIUrsDmd29Uvj096u2yW/USLB/4cene26V1TtQGRFYDtL YYy8d8F71C6Gs+cBUvx2NQIeIVXqJU8MTApGxAzilJYdjEalnA/RE0newnuHoWsI RpEzOkrxRjD91RKZZV9c3GzF990a4dx7Kag37yWA7k8Lv7ieulWxCt4/OzhFxMdx bmGBCiQ4/FhgN/gc5FKAecyHxsdmuerDdOO/pG/t+LZWYmealBO7KL+D2UF2QSaM iPd5fpLDnW0X1Otlk2O9/Nm5LEq2DKdZphigq2JoR9CHvEsn0z8= =ysO7 -----END PGP SIGNATURE----- --taouumsq7jejofry--