pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: tango ward <tangoward15@gmail.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: reduce number of multiple values to be inserted
Date: Tue, 29 May 2018 17:36:47 -0700
Message-ID: <364282d1-ee65-544e-cd12-aae964db3f4e@aklaver.com> (raw)
In-Reply-To: <CAA6wQLLRSCkVrxmo23pc1sbyW0rr5SQX8bTfMXA3WpgmH=48rQ@mail.gmail.com>
References: <CAA6wQLLRSCkVrxmo23pc1sbyW0rr5SQX8bTfMXA3WpgmH=48rQ@mail.gmail.com>

On 05/29/2018 05:10 PM, tango ward wrote:
> 
> Hi,
> 
> I am working on inserting multiple values for a table. I need to insert 
> 3 values of data for each age of the students from the same village. It 
> will be 3 different ages of student per village.
> 
> My sample code:
> 
> 
> curr_pgsql.execute('''
>                     INSERT INTO student (created, modified, name,
>                                                      address, age, level
>                                                      )
>                     VALUES (current_timezone, current_timezone,
>                             'Scott', 'Malayan Village', 21, 2),
>                            (current_timezone, current_timezone,
>                             'Ben', 'Malayan Village', 21, 2),
>                            (current_timezone, current_timezone,
>                             'Scott', 'Malayan Village', 21, 2),
> 
>                            (current_timezone, current_timezone,
>                             'Andrew', 'Malayan Village', 25, 2),
>                            (current_timezone, current_timezone,
>                             'Larry', 'Malayan Village', 25, 2),
>                            (current_timezone, current_timezone,
>                             'Adam', 'Malayan Village', 25, 2),
> 
>                            (current_timezone, current_timezone,
>                             'Elisse', 'Malayan Village', 27, 2),
>                            (current_timezone, current_timezone,
>                             'Xena', 'Malayan Village', 27, 2),
>                            (current_timezone, current_timezone,
>                             'Karen', 'Malayan Village', 27, 2)
> 
> I will repeat the same process for 13 villages so that will be 117 of 
> values. I would like to know if there's a way to reduce the script? This 
> has to be done strictly via script.

Not sure where you are pulling the data from and how it is ordered, but 
an outline:

data_input (Assuming sorted by village and then age)

for village in data_input:
	for age in village:
		 curr_pgsql.execute('''
		 INSERT INTO student (created, modified, name,
                                       address, age, level)
                  VALUES(current_timezone, current_timezone,
                         %s, %s, %s, 2)''', (name, village, age))

> 
> 
> Thanks,
> J
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (10+ messages)  latest in thread

Message-ID: <364282d1-ee65-544e-cd12-aae964db3f4e@aklaver.com>
Permalink:  ../364282d1-ee65-544e-cd12-aae964db3f4e@aklaver.com/
Also on:    postgresql.org/message-id/364282d1-ee65-544e-cd12-aae964db3f4e@aklaver.com

 · 

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: adrian.klaver@aklaver.com, tangoward15@gmail.com
  Subject: Re: reduce number of multiple values to be inserted
  In-Reply-To: <364282d1-ee65-544e-cd12-aae964db3f4e@aklaver.com>
  References: <CAA6wQLLRSCkVrxmo23pc1sbyW0rr5SQX8bTfMXA3WpgmH=48rQ@mail.gmail.com> <364282d1-ee65-544e-cd12-aae964db3f4e@aklaver.com>

* 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