pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: case statement within insert
Date: Mon, 28 May 2018 11:34:51 +0200
Message-ID: <20180528093451.7qxudqsoncbexcvu@hjp.at> (raw)
In-Reply-To: <CAA6wQLJ7Ygy66GnB_Akc+GDcm6LbHt3XD9zX92Y7PiBRXZ=12A@mail.gmail.com>
References: <CAA6wQLJ7Ygy66GnB_Akc+GDcm6LbHt3XD9zX92Y7PiBRXZ=12A@mail.gmail.com>

On 2018-05-25 17:04:25 +0800, 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%%'
>         THEN True
>         ELSE False
>     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 = '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


-- 
   _  | 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 <https://www.edge.org/;

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (8+ messages)

Message-ID: <20180528093451.7qxudqsoncbexcvu@hjp.at>
Permalink:  ../20180528093451.7qxudqsoncbexcvu@hjp.at/
Also on:    postgresql.org/message-id/20180528093451.7qxudqsoncbexcvu@hjp.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hjp-pgsql@hjp.at
  Subject: Re: case statement within insert
  In-Reply-To: <20180528093451.7qxudqsoncbexcvu@hjp.at>
  References: <CAA6wQLJ7Ygy66GnB_Akc+GDcm6LbHt3XD9zX92Y7PiBRXZ=12A@mail.gmail.com> <20180528093451.7qxudqsoncbexcvu@hjp.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox