tags:

views:

122

answers:

2

Hello, 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
A: 

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+")")
ghostdog74
Don't format the string by hand: http://wiki.python.org/moin/DbApiFaq
Bastien Léonard
+1  A: 

Try

curs.execute("insert into location_tr values\
        (NULL, '%s', '%s')" % (data.entries[i].title, data.feed.updated))
jellybean
Don't format the string by hand: http://wiki.python.org/moin/DbApiFaq
Bastien Léonard
@jellybean: Thanks for your answer!...:D
rahman.bd
@bastien: Thx for the hint
jellybean
@bastien: Thanks to you as well for hints!
rahman.bd
@jellybean: I have facing another problem!..after couple of running above program , I got error on conn.commit().It says syntax error on conn.commit(). But it was not problem earlier after fixing the error as you told !..Can you tell me why conn.commit() shows error?
rahman.bd
I can only guess ... maybe you forgot to close a bracket in the preceding line?
jellybean
@jellybean: yes you are right, I forgot to add bracket in the preceding line !Another issue:As it is from same code so I dont feel necessity to have a new question again! My resulting feed contents from above code has some unicode characters which need to use UTF-8.But can you please tell me how to use UTF-8 for above mentioned code example?Thanks in advance!
rahman.bd