2010-02-17 66 views
0

我想在python.But中面臨error.Could存儲一些解析的feed內容值在Sqlite數據庫表中。有人能幫我解決這個問題。事實上這是如此微不足道的問題要問!我是新手! ..無論如何感謝提前!在Python中使用SQLite3

from sqlite3 import * 
import feedparser 

data = feedparser.parse("some url") 

conn = connect('location.db') 
curs = conn.cursor() 

curs.execute('''create table location_tr 
    (id integer primary key, title text , 
     updated text)''') 


for i in range(len(data['entries'])): 
    curs.execute("insert into location_tr values\ 
      (NULL, data.entries[i].title,data.feed.updated)") 
conn.commit() 
curs.execute("select * from location_tr") 
for row in curs: 
    print row 

和錯誤是:

Traceback (most recent call last): 
    File "F:\JavaWorkspace\Test\src\sqlite_example.py", line 16, in <module> 
    (NULL, data.entries[i].title,data.feed.updated)") 
sqlite3.OperationalError: near "[i]": syntax error 

回答

1

嘗試

curs.execute("insert into location_tr values\ 
     (NULL, '%s', '%s')" % (data.entries[i].title, data.feed.updated)) 
+2

不要格式化字符串手動:http://wiki.python.org/moin/DbApiFaq – 2010-02-17 13:29:08

+0

@jellybean:感謝您的回答!:D – 2010-02-17 13:36:57

+0

@bastien:Thx的提示 – 2010-02-17 13:44:44

0

誤差應在這一行

curs.execute("insert into location_tr values\ 
      (NULL, data.entries[i].title,data.feed.updated)") 

data.entries[i].title來自Python的。所以如果你用雙引號把它括起來,它就變成了一個文字字符串,而不是一個值。它應該是這樣的:

curs.execute("insert into location_tr values (NULL," + data.entries[i].title +","+data.feed.updated+")") 
+2

不要格式化手串:http://wiki.python.org/moin/DbApiFaq – 2010-02-17 13:29:25