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 1fNpAE-0000KL-IW for pgsql-general@arkaria.postgresql.org; Wed, 30 May 2018 00:40:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fNpAC-0001cN-RH for pgsql-general@arkaria.postgresql.org; Wed, 30 May 2018 00:40:28 +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 1fNp6j-00046m-Mm for pgsql-general@lists.postgresql.org; Wed, 30 May 2018 00:36:55 +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 1fNp6f-0004Lh-Oi for pgsql-general@lists.postgresql.org; Wed, 30 May 2018 00:36:51 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id B85DC21B1C; Tue, 29 May 2018 20:36:48 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 29 May 2018 20:36:48 -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=YX7t8CdXsJnhZ5jkGuPVc1J7G/7Ge JYWBBaEExZPwUE=; b=SixNncb1051OE8mcj/a1lkGgdSztO8xd30005f4oEEAX7 iv9hKweci25qYLEeN/xiE7TLqq4hKfCxMBAsoZ2qfrkruwq6EyCJ6aOjZGqDEG2R No3OlPfDTPGy9dlbctUaEXaFBGQyy74i0sJCzukPdTuWrxs10pYqVtt2KtoMNxB0 uR3Nv8p9024oo3L+eEyEI09jkn11gD3/BI9HJRWzphUlmOFoOTfLLRcBLGxdzdC2 /0Qrqy557Ie24ymp6Y/eRdxf8z8F0Z1EZMjQtDLZYKlZ5jB1sl9Gax9cGBj2nZC2 3SmOIlGgok0eg+bCRwoNglrAQoBp8xd3JO0eUF7wA== 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=YX7t8C dXsJnhZ5jkGuPVc1J7G/7GeJYWBBaEExZPwUE=; b=Yelfn/BUrICa24AbO97yGM /KAE4qGBRecfz87AgXOZY7DUKbUjWQzgOzEH1hVp4mBzsuH6NYkys9YHmeHVG3bS WZSMdVTsO2SVhPOqIKro2VWyJOO2UP189ck9rxKyMXv2eUxz9BMEXNRF0rCrgVF5 cLSB/wgjJda/LkAo54eZdqsBLcmuSCe5W8Gb2s2wjZMHcpQYZfvPGxpI7JnkS/WX GHNZS834LwC7tMW4Rd9N5Jhpm1MI7pfgopzNhwQuvePRpDbJ697ez1flD6LeuE/M 4eZGmPb1Gr3Hp3Qn9yywwp4nmyTS5aSdcAlc9YeKXjIuxKy9vuTy9c9EerRB0wVQ == 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.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id 127291025C; Tue, 29 May 2018 20:36:47 -0400 (EDT) Subject: Re: reduce number of multiple values to be inserted To: tango ward , "pgsql-generallists.postgresql.org" References: From: Adrian Klaver Message-ID: <364282d1-ee65-544e-cd12-aae964db3f4e@aklaver.com> Date: Tue, 29 May 2018 17:36:47 -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: 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/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