Re: Insert data if it is not existing

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: tango ward <tangoward15(at)gmail(dot)com>
Cc: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Insert data if it is not existing
Date: 2018-05-24 01:33:44
Message-ID: CAKFQuwZ7StBgVPoFLiCUFgdCXsJmXLUOj26U+ymWw4e03_R02g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wednesday, May 23, 2018, tango ward <tangoward15(at)gmail(dot)com> wrote:

>
> On Thu, May 24, 2018 at 9:09 AM, David G. Johnston <
> david(dot)g(dot)johnston(at)gmail(dot)com> wrote:
>
>> On Wednesday, May 23, 2018, tango ward <tangoward15(at)gmail(dot)com> wrote:
>>
>>>
>>>
>>> curr.pgsql.execute('''
>>> INSERT INTO my_table(name, age)
>>> SELECT %s, %s
>>> WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= name)
>>> ''', ('Scott', 23))
>>>
>>
>> So, WHERE name = name is ALWAYS true and so as long as there is at least
>> one record in my_table the exists returns true, and the not inverts it to
>> false and the main select returns zero rows. You have successfully
>> inserted a record that doesn't exist (i.e., you've inserted nothing just
>> like you observe).
>>
>> David J.
>>
>
> Any advice on this Sir? Even adding the FROM statement in SELECT statement
> doesn't insert the data
>
>
> INSERT INTO my_table(name, age)
> SELECT %s, %s
> FROM my_table
> WHERE NOT EXISTS(SELECT name from my_table WHERE name = name)''',
> ('Scott', 23)
>
> I also need to perform the same task but on another table but the data for
> that is from another DB.
>

I advise you fiddle with it some more and see if you can stumble upon a
functioning solution. Maybe step away from the problem for a bit, get some
fresh air, maybe sleep on it. You've demostrated knowledge of the various
parts that will make up the solution, and have been given more in the rest
of this thread, and figuring out how they go together is something you will
either get, or not.

Or wait for a less harsh person to give you the answer and move you forward
to the next beginner's problem.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message tango ward 2018-05-24 01:36:30 Re: Insert data if it is not existing
Previous Message tango ward 2018-05-24 01:23:30 Re: Insert data if it is not existing