Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import argparse
import sqlite3 as lite
import os
def _open_db(db):
con = lite.connect(db)
cur = con.cursor()
return (cur,con)
except lite.Error as e:
print("Could not open database %s: %s" % (db,e))
if __name__=="__main__":
parser = argparse.ArgumentParser(description='delete duplicates in XBMC/Kodi video database')
parser.add_argument('db', help='path to MyVideos**.db library')
args = parser.parse_args()
(cur,con) = _open_db(args.db)
# list all duplicates in table 'path'
cur.execute('SELECT idPath, strPath, strHash, count(*) FROM path GROUP BY strPath, strHash having count(*) > 1')
result = cur.fetchmany()
duplicates = []
while result:
for idPath, strPath, strHash, count in result:
result = cur.fetchmany()
print '%d duplicated items found!' % len(duplicates)
# delete all duplicates from table 'path'
for i in range(len(duplicates)):
print 'delete item with idPath %s from table path' % duplicates[i]
cur.execute("DELETE FROM path WHERE idPath=?", (duplicates[i],))
# clean progress line
print "\r" + 100*" " + "\r",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment