mardi 27 octobre 2015

Scrapy - Error binding parameter when writing item to sqlite table

When trying to insert the contents of a Scrapy item to a SQLite table, I get an error binding parameter

2015-10-27 22:57:09 [scrapy] DEBUG: Crawled (200) <GET http://ift.tt/1ieHp07; (referer: http://ift.tt/1ieHrVx)
<type 'list'>
2015-10-27 22:57:09 [scrapy] ERROR: Error processing {'channel': [u'PRIME STAR'],
 'rating': [u'6.9'],
 'start_ts': [u'13:00'],
 'title': 'Ida'}
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/defer.py", line 577, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/Users/bertcarremans/Documents/Python/topfilms/topfilms/pipelines.py", line 15, in process_item
    self.storeInDb(item)
  File "/Users/bertcarremans/Documents/Python/topfilms/topfilms/pipelines.py", line 31, in storeInDb
    item['rating']
InterfaceError: Error binding parameter 1 - probably unsupported type.

In pipelines.py, I wrote the function storeInDb to store the item:

def storeInDb(self, item):
    self.cur.execute("INSERT INTO topfilms(\
        title, \
        channel, \
        start_ts, \
        rating \
        ) \
    VALUES( ?, ?, ?, ? )",
    (
        item['title'],
        item['channel'],
        item['start_ts'],
        item['rating']
    ))
    self.con.commit()

And the table is create as follows:

def createTopFilmsTable(self):
    self.cur.execute("CREATE TABLE IF NOT EXISTS topfilms(id INTEGER PRIMARY KEY NOT NULL, \
        title TEXT, \
        channel TEXT, \
        start_ts TEXT, \
        rating TEXT \
        )")

What format does rating need to be in order to have the code working? Thanks!

Aucun commentaire:

Enregistrer un commentaire