forked from sumedha3111/Python-for-Beginners
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.py
More file actions
Latest commit
34 lines (27 loc) · 941 Bytes
/
Copy pathsqlite.py
File metadata and controls
34 lines (27 loc) · 941 Bytes
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
importsqlite3
conn=sqlite3.connect('sql_mailcount.sqlite')
cur=conn.cursor()
cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('''
CREATE TABLE Counts (org TEXT, count INTEGER)''')
fname=input('Enter file name: ')
if (len(fname) <1): fname='mbox-short.txt'
fh=open(fname)
forlineinfh:
ifnotline.startswith('From: '): continue
pieces=line.split('@')
domain=pieces[1]
cur.execute('SELECT count FROM Counts WHERE org = ? ', (domain,))
row=cur.fetchone()
ifrowisNone:
cur.execute('''INSERT INTO Counts (org, count)
VALUES (?, 1)''', (domain,))
else:
cur.execute('UPDATE Counts SET count = count + 1 WHERE org = ?',
(domain,))
conn.commit()
# https://www.sqlite.org/lang_select.html
sqlstr='SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10'
forrowincur.execute(sqlstr):
print(str(row[0]), row[1])
cur.close()