Finally i made it!! Here it is:

# =================================================================================================================
# Have 1:1 mapping of files <-> database records, delete spurious
# =================================================================================================================
filenames = []

# Turn files from bytestrings => strings and trim them from their paths
for utf8_filename in utf8_filenames:
filenames.append( utf8_filename.decode('utf-8').replace( '/home/nikos/public_html/data/apps/', '' ) )

# Create a database entry for each file
for filename in filenames:
        try:
                # Try to insert the file into the database
cur.execute('''INSERT INTO files (url, host, lastvisit) VALUES (%s, %s, %s)''', (filename, host, lastvisit) )
        except pymysql.IntegrityError as e:
# Insertion failed, so, file already exist into database, skip this, go to next
                pass

# Delete those database records that do not correspond to files
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()

for rec in data:
        if rec[0] not in filenames:
                cur.execute('''DELETE FROM files WHERE url = %s''', rec[0] )
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to