2010-02-17 36 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