简体   繁体   中英

Using SQLite3 in Python

I am trying to store some parsed feed contents values in Sqlite database table in python.But facing error.Could anybody help me out of this issue.Infact it is so trivial question to ask!I am newbie!..Anyway thanks in advance!

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

And Error is:

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

Try

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

the error should be this line

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

data.entries[i].title comes from Python. So if you enclose it in double quotes, it becomes a literal string, not a value. It should be something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM