我試圖添加它在解析每一行之後創建的列表。當我經過每一個代碼,我得到不同的錯誤將Python列表輸入到SQLite中
(C:\Users\myname\Desktop\pythonCourse>dblesson2
Enter file name: mbox.txt
['uct.ac.za']
Traceback (most recent call last):
File "C:\Users\myname\Desktop\pythonCourse\dblesson2.py", line 25, in
<module>
#VALUES (?, 1)''', (email,))
sqlite3.OperationalError: near "#VALUES": syntax error)
,我知道那是因爲我沒有正確的數據傳遞到數據庫,但我不知道這一點對我自己。
import sqlite
import re
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 = 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
line = line.rstrip()
email = re.findall('@(\S+[a-zA-Z]+)', line)
print email
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,))
# 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 email, count FROM Counts ORDER BY count DESC LIMIT 10'
print
print "Counts:"
for row in cur.execute(sqlstr) :
print str(row[0]), row[1]
cur.close()`
歡迎堆棧溢出!你能否澄清問題是什麼? 「我得到不同的錯誤」不是很有幫助。發佈這些錯誤!請花點時間閱讀[我如何提出一個好問題?](http://stackoverflow.com/help/how-to-ask)。 – Carpetsmoker