-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemaildb.py
More file actions
50 lines (34 loc) · 1.13 KB
/
emaildb.py
File metadata and controls
50 lines (34 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 22:14:50 2018
@author: Jimit
"""
# Counting Email in a Database
import sqlite3
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('''
CREATE TABLE Counts(email TEXT, count INTEGER)''')
fname = input('Enter file name: ')
if(len(fname) < 1): fname = 'mbox-short.txt'
fh = open(fname)
for line in fh:
if not line.startswith('From: '): continue
pieces = line.split()
email = pieces[1]
cur.execute('SELECT count FROM Counts WHERE email = ?', (email,))
# (email,) is a one-element tuple
# (email) won't turn 'email' into a tuple
# so we need to add a comma after 'email'
row = cur.fetchone()
if row is None:
cur.execute('''INSERT INTO Counts(email, count)
VALUES (?, 1)''', (email,))
else:
cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?', (email,))
conn.commit()
sqlstr = 'SELECT email, count FROM Counts ORDER BY count DESC LIMIT 10'
for row in cur.execute(sqlstr):
print(str(row[0]), row[1])
cur.close()