心碎的Basic SQL&Assignment

来源:互联网 发布:python搜索引擎书籍 编辑:程序博客网 时间:2024/04/29 13:55
import sqlite3conn = 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 completesconn.commit()# https://www.sqlite.org/lang_select.htmlsqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC'cur.execute(sqlstr)cur.close()

除了SQL语句以外都是老东西,参考下注释把,顺便说明一下邮箱的域名!!!

64949432@qq.com
域名是qq.com!!!!!

0 0
原创粉丝点击