Re: Insert data if it is not existing - Mailing list pgsql-general

From Adrian Klaver
Subject Re: Insert data if it is not existing
Date
Msg-id c9b652fb-13d6-5a40-0ec4-68d12201e1ef@aklaver.com
Whole thread Raw
In response to Re: Insert data if it is not existing  (tango ward <tangoward15@gmail.com>)
Responses Re: Insert data if it is not existing
List pgsql-general
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 
> <david.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>> wrote:
> 
>     On Wednesday, May 23, 2018, tango ward <tangoward15@gmail.com
>     <mailto:tangoward15@gmail.com>> 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


pgsql-general by date:

Previous
From: tango ward
Date:
Subject: Re: Insert data if it is not existing
Next
From: "David G. Johnston"
Date:
Subject: Re: Insert data if it is not existing