在Python中使用SQLite3

时间:2010-02-17 13:17:47

标签: python sqlite

我试图在python中的Sqlite数据库表中存储一些已解析的feed内容值。但是面临错误。可以有人帮助我解决这个问题。实际上这是一个微不足道的问题!我是新手!无论如何谢谢提前!

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

2 个答案:

答案 0 :(得分:1)

尝试

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

答案 1 :(得分: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+")")