如何避免插入重複數據?我只想插入不存在的數據。我已經撰寫了以下查詢,但其工作不正常。我正在使用PostgreSQL
。如何避免在PostgreSQL中插入重複數據?
title_exits = cursor.execute ("SELECT title,pageid FROM movie_movie WHERE title = %s AND pageid = %s;",(title,pageid))
if title_exits == 0:
cursor.execute("INSERT INTO movie_movie (title,pageid,slug,language) values (%s,%s,%s,%s);",(title,pageid,slug,id))
db.commit()
更新:我試過result = cursor.fetchone ("SELECT count(*) FROM movie_movie WHERE title = %s AND pageid = %s;",(title,pageid))
。但是我收到錯誤消息。 TypeError: fetchone() takes not arugments (2 given).
那'cursor.execute'是來自psycopg2嗎? – madth3
你看過這個問題和決定嗎? [鏈接](http://stackoverflow.com/questions/2511679/python-number-of-rows-affected-by-cursor-executeselect) – tony
@ madth3是'cursor = db.cursor()' –