Created
July 2, 2012 22:22
-
-
Save papachan/3036113 to your computer and use it in GitHub Desktop.
eliminate duplicate rows in table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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