I’m using sqlite with python. I’m implementing the POP3 protocol. I have a table
msg_id text date text from_sender text subject text body text hashkey text
Now I need to check for duplicate messages by checking the message id of the message retrieved against the existing msg_id’s in the table. I encrypted the msg_id using md5 and put it in the hashkey column. Whenever I retrieve mail, I hash the message id and check it with the table values. Heres what I do.
def check_duplicate(new):
conn = sql.connect("mail")
c = conn.cursor()
m = hashlib.md5()
m.update(new)
c.execute("select hashkey from mail")
for row in c:
if m.hexdigest() == row:
return 0
else:
continue
return 1
It just refuses to work correctly. I tried printing the row value, it shows it in unicode, thats where the problem lies as it cannot compare properly.
Is there a better way to do this, or to improve my method?
Well, if your only problem is with the comparison, then you could try:
since
rowis a tuple and not a string, but your basic strategy seems wrong to me. You’re retrieving thehashkeyfor every row from the database, and then doing your own search for the right one. Much better to make the database do the search for you. The database is likely to be better at searching (since it probably has an index on thehashkeyfield—you did create an index for this field, didn’t you?) and it only has to send one result to you, saving time. So you could issue a query like this to determine if the message exists:A final point of style: Python has
TrueandFalse, so there’s no need to use1and0for Booleans.