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 1fLdym-0006Lp-29 for pgsql-general@arkaria.postgresql.org; Thu, 24 May 2018 00:19:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fLdyk-0007ID-VI for pgsql-general@arkaria.postgresql.org; Thu, 24 May 2018 00:19:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fLdyk-0007I4-NE for pgsql-general@lists.postgresql.org; Thu, 24 May 2018 00:19:38 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fLdyb-0005KA-T3 for pgsql-general@lists.postgresql.org; Thu, 24 May 2018 00:19:38 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 8E02921E4D; Wed, 23 May 2018 20:19:28 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 23 May 2018 20:19:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc: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=hixn0wAypYzTv8pD80fhvCu51e5kh G/aiSfZd7Wcdq4=; b=Iy40gWBZbNf3kyaxpBG8kw/31noFdP5iMnZm4zb5X2H44 4FQEdTDqN5gAGdktuoQkipct81ACFZ9B+W2rHQwFTjS1RElA6g9vtJTZDq6UxZi1 wkgdvee9flz6CWbafZpZ6njp+mDC06GmmVTSJN8hEqfrMdEX9uvAd26ef1qO1qLb TiZ3qPVnvNIZ3kRD8tLWBTb8+PT3Xhn+DUOlEQt8nVuu9h6SAxfaJw9vjy54i6P7 6NLIBXB28Q/4Z2kTyFVl8O4F3BcNA5uuFRttKnMaWtwPIKjlkP24Hon/T9jr5LeP wHP0amUeUfnkmjoE4nzJW3OfRBpIindkZR6Oe3L6g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=hixn0w AypYzTv8pD80fhvCu51e5khG/aiSfZd7Wcdq4=; b=ZTdihU3i4rHSV/blSLb6WU fLtmzEbBSZZC2Y32k2Xx0v+OACydS0qkzYlTXN0hXnWzoQ2X961lOfNKIHloTT1u QOnT9vZ9rINTs6t7t3DSv6iCC5UsszG8er0ZzIG/BJS0UOvwBPc00NE5MNWViyzL /iw/mLKENzbwIiPnaKa0XF71cI1I+wsWh6lfYgA19ynFnDGJ21lAMOgMkcVUIKoV kQ+hX/jg/hrI9Wy2HfBGIhMRKpvw5e91+t/ystJ97WD6FFY1CUynQ+MPkU/mU0v1 /vNiCkGel8L0wUOtc8jDOzPq1xiS3yZn1HPjrdtFO2n8vpTTsfc2pVonyZxylXNQ == 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 F1163E4437; Wed, 23 May 2018 20:19:27 -0400 (EDT) Subject: Re: Insert data if it is not existing To: tango ward , "David G. Johnston" Cc: "pgsql-generallists.postgresql.org" References: <0a76f762-88d8-75a7-de3c-d76094638fdd@aklaver.com> From: Adrian Klaver Message-ID: Date: Wed, 23 May 2018 17:19:27 -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/23/2018 05:11 PM, tango ward wrote: > Sorry, i forgot the values. > > curr.pgsql.execute(''' > INSERT INTO my_table(name, age) > SELECT name, age > WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= name) > ''', ('Scott', 23)) Pretty sure this would throw an exception as there are no parameter markers in the query for the parameter values in the tuple to bind to. So are you swallowing the exception in you code? > > Sorry, I don't understand, where should I place the from clause? I just > saw a sample code like this in SO, so I gave it a shot Not tested: ''' INSERT INTO my_table(%(name)s, %(age)s) WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= %(name)) ''', {'name': Scott', 'age': 23}) > > > On Thu, May 24, 2018 at 8:04 AM, David G. Johnston > > wrote: > > On Wednesday, May 23, 2018, tango ward > wrote: > > Thanks masters for responding again. > > I've tried running the code: > > INSERT INTO my_table(name, age) > SELECT name, age > WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= name) > > > this doesn't give me error but it doesn't insert data either. > > > I'm doubting your assertion that it doesn't error.   How do you run > that query such that age and name are recognized given the main > query doesn't have a from clause? > > David J. > > -- Adrian Klaver adrian.klaver@aklaver.com