import sqlite3
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
#conn有点像文件的句柄
cur.execute('''
DROP TABLE IF EXISTS Counts''')
cur.execute('''
CREATE TABLE Counts (org TEXT, count INTEGER)''')
fname = raw_input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'mbox-short.txt'
fh = open(fname)
for line in fh:
if not line.startswith('From: ') : continue
email = line.split('@')[1].rstrip()#这里的rstrip()不能另外写一行
print email
cur.execute('SELECT count FROM Counts WHERE org = ? ', (email, ))
row = cur.fetchone()
if row is None:
cur.execute('''INSERT INTO Counts (org, count)
VALUES ( ?, 1 )''', ( email, ) )
else :
cur.execute('UPDATE Counts SET count=count+1 WHERE org = ?',
(email, ))
# This statement commits outstanding changes to disk each
# time through the loop - the program can be made faster
# by moving the commit so it runs only after the loop completes
conn.commit()
# https://www.sqlite.org/lang_select.html
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC'
cur.execute(sqlstr)
cur.close()
除了SQL语句以外都是老东西,参考下注释把,顺便说明一下邮箱的域名!!!
64949432@qq.com
域名是qq.com!!!!!