2016-03-10 67 views
1

我試圖添加它在解析每一行之後創建的列表。當我經過每一個代碼,我得到不同的錯誤將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()` 
+0

歡迎堆棧溢出!你能否澄清問題是什麼? 「我得到不同的錯誤」不是很有幫助。發佈這些錯誤!請花點時間閱讀[我如何提出一個好問題?](http://stackoverflow.com/help/how-to-ask)。 – Carpetsmoker

回答

0

您的程序中有許多小錯誤。讓我嘗試列出它們:

  • re.findall返回一個列表,但您似乎將它視爲單個字符串。嘗試email = email[0]只考慮列表的第一個元素。
  • 您的第一個SELECT聲明有(email)。將一個項目放在圓括號內不會使其成爲一個元組。改爲嘗試使用(email,)[email]
  • for循環的每個迭代中,for循環後面的if必須縮進一個循環。
  • if的正文不能爲空。請取消註釋該操作,或將其更改爲pass
  • 您最後的for循環的正文需要縮進一格。
  • 作爲Stack Overflow讀者的禮貌,請複製粘貼整個獨立程序,而不僅僅是摘錄。

這裏是你的程序後,我固定的問題:

import sqlite3 
import re 

conn = sqlite3.connect(':memory:') 
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) 
    email = email[0] 
    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() 
+0

這就是爲什麼我喜歡這個社區爲有幫助的人喜歡你自己!我將閱讀並重新閱讀您的評論,以更充分地理解這​​一點。我很感激你花費在幫助我理解這個問題上的時間。希望將來我可以像今天一樣延伸自己。再次感謝你 :) –

0
import sqlite3 
conn=sqlite3.connect('emaildb.sqlite') 
cur=conn.cursor() 

cur.execute('''DROP TABLE IF EXISTS counts''') 
cur.execute('''CREATE TABLE counts (org TEXT, count INTEGER)''') 

f_name=raw_input('Enter file name: ') 
if len(f_name)<1 : f_name='mbox.txt' 
fn=open(f_name) 
for line in fn: 
    if not line.startswith('From: ') : continue 
    words = line.split() 
    email=words[1] 
    domain=email.split('@') 
    organiz=domain[1] 
    print organiz 
    cur.execute('SELECT count FROM Counts WHERE org=?',(organiz,)) 
    row=cur.fetchone() 
    if row==None: 
    cur.execute('''INSERT INTO counts (org, count) VALUES (?,1)''', 
    (organiz,)) 
    else: 
    cur.execute('''UPDATE counts SET count=count+1 WHERE org=?''',(organiz, 
    )) 
conn.commit()