2011-11-22 118 views
0

只有在數據庫中存在does not時,我如何將這些artist插入到數據庫中。我寫了下面的代碼。但這是錯誤的。你能幫我嗎?感謝避免重複數據PostgreSQL

藝術家名單

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

我的代碼

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 

輸出:

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; 

輸出應爲:

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; 

更新:

我已經添加了對列名的約束來強制該列是唯一的。

回答

1

你必須使用if not artist_rows:這是全部

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() 
2
  1. 強制在數據庫端。在列名上添加約束以強制該列是唯一的。這應該足夠了:

    ALTER TABLE song_artist ADD UNIQUE(name);

    這樣你就可以確定無論你做什麼,你都不會有任何重複的值。

  2. 你可以插入使用查詢這樣的數據:

    INSERT INTO song_artist(名稱,蛞蝓)
    SELECT '馬亨德拉·卡普爾',「馬亨德拉 - 卡普爾
    WHERE NOT EXISTS

    SELECT 42
    FROM song_artist
    WHERE name ='Mahendra Kapoor'
    );

+0

這是com折襞。什麼是42? – Kulbir

+0

我試過你的代碼。但不工作。獲取錯誤消息。 'psycopg2.IntegrityError:重複鍵值違反唯一約束「song_ar tist_slug_key」 詳細信息:鍵(slug)=(lata-mangeshkar)已存在。「# – Kulbir

+0

這是因爲您已經有重複的數據。刪除重複項並重試。 –

0

您可以添加唯一的約束作爲Szymon GUZ建議,

ALTER TABLE song_artist ADD UNIQUE (name); 

然後使用try/except來處理重複的錯誤:

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'