0

How do i insert these artist into database only if does not exist in database. I have written following code. But it is wrong. Will you please help me ? thanks

Artist List

Lata Mangeshkar  
Lata Mangeshkar
Lata Mangeshkar
Asha Bhosle
Lata Mangeshkar
Mahendra Kapoor  
Lata Mangeshkar
Kishore Kumar
Lata Mangeshkar

My code

for l in artist:
        l = l.strip()
        cursor.execute("SELECT id FROM song_artist WHERE name = %s ;" ,(l,) ) # Id exist?
        artist_rows = cursor.fetchone()
        if not artist_rows:
            artist_slug = self.makeSlug(artist[0])
            cursor.execute( 'INSERT INTO song_artist (name,slug) VALUES (%s,%s) RETURNING id;',(artist[0],artist_slug))
            print k

Output :

INSERT INTO song_artist (name,slug) VALUES (Lata Mangeshkar,lata-mangeshkar) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Lata Mangeshkar,lata-mangeshkar) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Lata Mangeshkar,lata-mangeshkar) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Lata Mangeshkar,lata-mangeshkar) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Asha Bhosle,asha-bhosle) RETURNING i
d;
INSERT INTO song_artist (name,slug) VALUES (Lata Mangeshkar,lata-mangeshkar) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Mahendra Kapoor,mahendra-kapoor) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Mahendra Kapoor,mahendra-kapoor) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Lata Mangeshkar,lata-mangeshkar) RET
URNING id;

OUTPUT SHOULD BE:

INSERT INTO song_artist (name,slug) VALUES (Asha Bhosle,asha-bhosle) RETURNING i
d;
INSERT INTO song_artist (name,slug) VALUES (Lata Mangeshkar,lata-mangeshkar) RET
URNING id;
INSERT INTO song_artist (name,slug) VALUES (Mahendra Kapoor,mahendra-kapoor) RET
URNING id;

UPDATE:

I have already Added constraint on the column name to enforce the column is unique.

3 Answers 3

2
  1. Enforce that on the database side. Add constraint on the column name to enforce the column is unique. This should be enough:

    ALTER TABLE song_artist ADD UNIQUE (name);

    This way you can be sure that whatever you do, you won't have any duplicated values.

  2. You could insert the data using queries like this:

    INSERT INTO song_artist (name, slug)
    SELECT 'Mahendra Kapoor','mahendra-kapoor'
    WHERE NOT EXISTS
    (
    SELECT 42
    FROM song_artist
    WHERE name = 'Mahendra Kapoor'
    );

Sign up to request clarification or add additional context in comments.

3 Comments

It is complicated. what is 42?
I tried your code. But not working. Getting error message. psycopg2.IntegrityError: duplicate key value violates unique constraint "song_ar tist_slug_key" DETAIL: Key (slug)=(lata-mangeshkar) already exists.
That's because you already have duplicated data. Delete the duplicates and try again.
0

You have to use if not artist_rows: That's all

    artist_slug = self.makeSlug(l)

cursor.execute("SELECT count(id) FROM song_artist WHERE name = %s and slug = %s ;" ,(l,artist_slug) ) # Id exist?
artist_rows = cursor.fetchone()
#artist_rows = artist_rows[0]
db.commit()
if not artist_rows:
cursor.execute( 'INSERT INTO song_artist (name,slug) VALUES (%s,%s) RETURNING id;',(l,artist_slug))
#k=( 'INSERT INTO song_artist (name,slug) SELECT %s,%s WHERE NOT EXISTS ( SELECT 42 FROM song_artist WHERE name = %s) RETURNING id;'%(l,artist_slug,l))
artist_id = cursor.fetchone()[0] # get mysql_insert_id using RETURNING
db.commit()

Comments

0

You can add the unique constraint as Szymon Guz suggested,

ALTER TABLE song_artist ADD UNIQUE (name);

Then use a try / except to handle the duplicate error:

try:
  cursor.execute( 'INSERT INTO song_artist (name,slug) VALUES (%s,%s) RETURNING id;',(artist[0],artist_slug))

except psycopg2.IntegrityError:
  debug.print 'Duplicate values found.  Insert was not successful'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.