I am trying to create a datebase named "Counts" which keeps track of how many times a spesific email address has sent an email. The data is parsed from a textfile and the database is created using python.
My code looks like this:
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: ')
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,))
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,))
cur.execute('SELECT * FROM Counts ORDER BY count DESC')
conn.commit()
The code produces perfectly working database, but it is not ordered. Using the above mentioned
`"SELECT * FROM Counts ORDER BY count DESC"`
command I can sort the database in the sqlitebrowser. Somehow that doesn't work when integrated in the python code even though I use commit after ordering command.
Thanks in advance.