def load_into_db(self, table):
def callback(response):
""" Loads the CSV data contained into the response body and puts it into the appropriate table
"""
data = itertools.ifilter(lambda x: len(x) == 2,
(row.split(',', 1) for row in response.body_as_unicode().split('\r\n')[1:]))
cur = self.db_connection.cursor()
try:
cur.executemany('INSERT INTO %s(pk, data) VALUES (?, ?)' % table, data)
except sqlite3.Error:
self.db_connection.rollback()
raise
else:
self.db_connection.commit()
finally:
cur.close()
for each in self.finalize_data(table):
yield each
return callback
评论列表
文章目录