SQLite

SQLite
http://www.sqlite.org

https://docs.python.org/3.5/library/sqlite3.html

import sqlite3

conn = sqlite3.connect(’emaildb.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.txt’
fh = open(fname)
for line in fh:
____if not line.startswith(‘From: ‘) : continue
pieces = line.split()
email = pieces[1]
organization = email.split(‘@’)[1]
cur.execute(‘SELECT count FROM Counts WHERE org = ? ‘, (organization, ))
row = cur.fetchone()
if row is None:
____cur.execute(”’INSERT INTO Counts (org, count)
____VALUES ( ?, 1 )”’, ( organization, ) )
else :
____cur.execute(‘UPDATE Counts SET count=count+1 WHERE org = ?’,
(organization, ))
print(organization)
conn.commit()

sqlstr = ‘SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10’

for row in cur.execute(sqlstr) :
____print(str(row[0]), row[1])

cur.close()

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s