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 1fLgTP-0008OB-K3 for pgsql-general@arkaria.postgresql.org; Thu, 24 May 2018 02:59:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fLgTO-00005g-Eu for pgsql-general@arkaria.postgresql.org; Thu, 24 May 2018 02:59:26 +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 1fLgTO-00005R-36 for pgsql-general@lists.postgresql.org; Thu, 24 May 2018 02:59:26 +0000 Received: from mail-oi0-x229.google.com ([2607:f8b0:4003:c06::229]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fLgTL-0002RW-IC for pgsql-general@lists.postgresql.org; Thu, 24 May 2018 02:59:24 +0000 Received: by mail-oi0-x229.google.com with SMTP id w123-v6so136524oia.4 for ; Wed, 23 May 2018 19:59:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=hNmECyylVT5PXyK+U1ChtyHZ7NiweOuJHm5h64PDtAc=; b=XUtiKCGzMOR6lJNSjN9W0XKyiH09U/95V20bgdkBVNMLz1iXt9HAiT9dJdobITzCUo c2qMhcQkIxKj3NHC4Wb8z17JQd0GSJlZnmEYU1TVzsr+1ha2mG6UcuzDryIcEeExUK2W R0aspvstZl23fkLo5gw4qUY9bA1agQzfToeT4lSCzrM3QssFxt4lkKhhmsY4oTYtnGUY 0r2xsm+asWH5NC4V4+yuSokml3Bm1fE6rnnP1ly8EyIuTBkl99BN3frugXqdy1pxniEf tEee24osfzAq3foNMCx/2jew69UIc7+cRBbkkezrPsfWAVNiNHgVZZKZpd/BwxsC3T8z R1rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=hNmECyylVT5PXyK+U1ChtyHZ7NiweOuJHm5h64PDtAc=; b=rqZb4FjYFiU58asXvNlYIttGq+1BLFnST33r3RiImEGynz3Rg8BI4je53oEOyUAv04 laAsa1AOf160qlk8tRdFn8rYA2+YmgE1/gEKcBTFyfyxXKidAMd+BSkKDPJBdS6Jddjd nI0tgDeiwfvyuOq5+79v0GntwZrE7Vvpy+XAONAGgau6NwnJmjalfT146CoJPZB4ZhkC Yg+RAJ44vklVDsud6sOeEKoX36jQ3JuMRx6ifLKqj0dEIVE+JkUNhYsAp3lydLuNGGrk qn2yMf0Y5xd/Q1jYv2N3DEqpkLMOo6YY/jH1KWSkmMp2mC4hluTV0M9mxtHQhiLvkCBE OoIA== X-Gm-Message-State: ALKqPwd0zhsJfT9YyI79h4xyBDj4D0k3qCJBXwV11sNGwnCwuAN2F56m s8pM34ljbpvr+K863ikM4It/6N92ZFPLiMSUn1g= X-Google-Smtp-Source: AB8JxZozvM7xFJWy65QpX5DAboASNmzYIEFj4Gjy6CGDHKvsHJUmmRhHTL9OxI8Vd0opC68xKWrqC9osbLxt6/m6zk0= X-Received: by 2002:aca:cec1:: with SMTP id e184-v6mr3094019oig.206.1527130762783; Wed, 23 May 2018 19:59:22 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a4a:9e18:0:0:0:0:0 with HTTP; Wed, 23 May 2018 19:59:22 -0700 (PDT) In-Reply-To: References: <0a76f762-88d8-75a7-de3c-d76094638fdd@aklaver.com> From: tango ward Date: Thu, 24 May 2018 10:59:22 +0800 Message-ID: Subject: Re: Insert data if it is not existing To: Adrian Klaver Cc: "David G. Johnston" , "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000a42f78056cead62d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000a42f78056cead62d Content-Type: text/plain; charset="UTF-8" On Thu, May 24, 2018 at 10:51 AM, Adrian Klaver wrote: > On 05/23/2018 06:03 PM, tango ward wrote: > >> >> On Thu, May 24, 2018 at 8:19 AM, Adrian Klaver > > wrote: >> >> 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}) >> -- Adrian Klaver >> adrian.klaver@aklaver.com >> >> >> >> >> Updated my code to this: >> >> curr.pgsql.execute(''' >> INSERT INTO my_table(name, age) >> SELECT %s, %s >> > > The above is not going to work as you cannot use %s to substitute for > identifiers, in this case the column names name and age. > > > WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= name) >> ''', ('Scott', 23)) >> >> If I remove SELECT statement, I will get an error message: error : >> psycopg2.ProgrammingError: syntax error at or near "WHERE" >> LINE 12: WHERE NOT EXISTS >> > > Try the example I showed previously. If you do not want to use the the > named parameters e.g %(name)s then use use %s and a tuple like: > > ''' > INSERT INTO my_table(%s, %s) > WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= %s) > ''', (Scott', 23, 'Scott')) > > > > > >> Trying to coordinate with Lead Dev about adding Index On The Fly >> >> >> >> > > -- > Adrian Klaver > adrian.klaver@aklaver.com > Thank you Master, the name=%s solved it. --000000000000a42f78056cead62d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable



On Thu, May 24, 2018 at 10:51 AM, Adrian Klaver &= lt;adrian.kl= aver@aklaver.com> wrote:
On= 05/23/2018 06:03 PM, tango ward wrote:

On Thu, May 24, 2018 at 8:19 AM, Adrian Klaver <adrian.klaver@aklaver.com <ma= ilto:adrian.= klaver@aklaver.com>> wrote:

=C2=A0 =C2=A0 On 05/23/2018 05:11 PM, tango ward wrote:

=C2=A0 =C2=A0 =C2=A0 =C2=A0 Sorry, i forgot the values.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 curr.pgsql.execute('''
=C2=A0 =C2=A0 =C2=A0 =C2=A0 INSERT INTO my_table(name, age)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 SELECT name, age
=C2=A0 =C2=A0 =C2=A0 =C2=A0 WHERE NOT EXISTS(SELECT name FROM my_table WHER= E name=3D name)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ''', ('Scott', 23))


=C2=A0 =C2=A0 Pretty sure this would throw an exception as there are no par= ameter
=C2=A0 =C2=A0 markers in the query for the parameter values in the tuple to= bind
=C2=A0 =C2=A0 to. So are you swallowing the exception in you code?



=C2=A0 =C2=A0 =C2=A0 =C2=A0 Sorry, I don't understand, where should I p= lace the from clause?
=C2=A0 =C2=A0 =C2=A0 =C2=A0 I just saw a sample code like this in SO, so I = gave it a shot


=C2=A0 =C2=A0 Not tested:
=C2=A0 =C2=A0 '''
=C2=A0 =C2=A0 INSERT INTO my_table(%(name)s, %(age)s)
=C2=A0 =C2=A0 WHERE NOT EXISTS(SELECT name FROM my_table WHERE name=3D %(na= me))
=C2=A0 =C2=A0 ''', {'name': Scott', 'age': = 23})
=C2=A0 =C2=A0 --=C2=A0 =C2=A0 =C2=A0Adrian Klaver
=C2=A0 =C2=A0 adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>




Updated my code to this:

curr.pgsql.execute('''
INSERT INTO my_table(name, age)
SELECT %s, %s

The above is not going to work as you cannot use %s to substitute for ident= ifiers, in this case the column names name and age.


WHERE NOT EXISTS(SELECT name FROM my_table WHERE name=3D name)
''', ('Scott', 23))

If I remove SELECT statement, I will get an error message: error : psycopg2= .ProgrammingError: syntax error at or near "WHERE"
LINE 12:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 WHERE NOT EXISTS

Try the example I showed previously. If you do not want to use the the name= d parameters e.g %(name)s then use use %s and a tuple like:

'''
INSERT INTO my_table(%s, %s)
WHERE NOT EXISTS(SELECT name FROM my_table WHERE name=3D %s)
''', (Scott', 23, 'Scott'))





Trying to coordinate with Lead Dev about adding Index On The Fly





--
Adrian Klaver
adrian.klave= r@aklaver.com

Thank= you Master, the name=3D%s solved it.
--000000000000a42f78056cead62d--