-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBASSIGN.py
32 lines (26 loc) · 940 Bytes
/
DBASSIGN.py
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
import sqlite3
import re
conn = sqlite3.connect('/home/ozne/Documents/GitHub/Python for Everybody/SQLite/assignment.sqlite')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('''CREATE TABLE Counts (org TEXT, count INTEGER)''')
fname = 'mbox.txt'
data = open(fname)
for line in data:
if not line.startswith('From: '): continue
domain = re.findall('@([^ ]*)', line)
temp = str(domain[0])
org = temp.strip()
cur.execute('SELECT count FROM Counts WHERE org = ? ', (org, ))
row = cur.fetchone()
if row is None:
cur.execute('''INSERT INTO Counts (org, count) VALUES (?, 1)''', (org, ))
else:
cur.execute('UPDATE Counts SET count = count + 1 WHERE org = ?', (org, ))
conn.commit()
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10'
for row in cur.execute(sqlstr):
dataa = str(row[0])
datab = str(row[1])
print(dataa, datab)
cur.close()