Skip to content

Instantly share code, notes, and snippets.

@papachan
Created July 2, 2012 22:22
Show Gist options
  • Save papachan/3036113 to your computer and use it in GitHub Desktop.
Save papachan/3036113 to your computer and use it in GitHub Desktop.
eliminate duplicate rows in table
import MySQLdb
def main(first):
sql = """SELECT * FROM `articles_autores`
ORDER BY article_id ASC
LIMIT %s"""
sql3 = """DELETE FROM `articles_autores` WHERE articles_autores.id = %s;"""
db = MySQLdb.connect(host='localhost', user='user',passwd='',db='test_duplicate');
cursor = db.cursor()
cursor.execute(sql % first)
rows = cursor.fetchall()
articles_id = 0
results = []
# secuencia
for row in rows:
if row[2] != articles_id:
results = []
articles_id = row[2]
if [row[1],row[2]] in results:
cursor.execute(sql3, (row[0]))
db.commit()
print 'duplicated:', str(row[0])
results.append([row[1],row[2]])
cursor.close()
db.close()
if(__name__ == '__main__'):
firstIndex = '0,100'
main(firstIndex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment