Reputation:
I have a python script that inserts rows into a table. I want it to either delete duplicate rows or ignore the INSERT if the row exists. What I have tried so far:
#delete duplicate entries
c.execute('''DELETE FROM server WHERE sites NOT IN
(SELECT MIN(sites) sites FROM server GROUP BY sites)''')
The table "server" is a single column "sites". This is a table of web sites. It's not throwing any errors. It's just doesn't not deleting the duplicates.
Upvotes: 0
Views: 1645
Reputation: 180010
Comparing against the sites
itself does not help, because the duplicates have the same value in this column.
You have to use some other column that is unique. In this case, you could use the rowid:
DELETE FROM server
WHERE rowid NOT IN (SELECT min(rowid)
FROM server
GROUP BY sites);
Upvotes: 1