From 3666bcf87a261912a2e9bc0cae5ab6c8b77c02c8 Mon Sep 17 00:00:00 2001 From: Vaishnavi Varshney Date: Thu, 1 Oct 2020 03:36:28 +0530 Subject: [PATCH] email-database-code --- email-database.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 email-database.py diff --git a/email-database.py b/email-database.py new file mode 100644 index 0000000..ce0c79b --- /dev/null +++ b/email-database.py @@ -0,0 +1,48 @@ +#This code is for making a database from given text File +#In this sample code I've taken a text file to extract the emails and the number +#of times they appear in text file. +#Ex - From: abc.cdbh@gmail.com +#index of email = 1 + + +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: ') +try: + fh = open(fname) +except: + print('File cannot be opened:', fname) + exit() + +for line in fh: + if not line.startswith('From: '): + continue + pieces = line.split() + email = pieces[1] #give the index from the text file + cur.execute('SELECT count FROM Counts WHERE email = ? ', (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() + +#printing required information +#Here, they are sorted in DESC ORDER +#and information is only asked for top 10 emails +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()