I am trying to store scraped data with scrapy to a sql database but I get the following error: ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘)’ at line 7
Please find my pipelines.py bellow
x
from datetime import datetime
import mysql.connector
from mysql.connector import errorcode
class LecturesinparisPipeline(object):
class CleaningPipeline(object):
class DatabasePipeline(object):
def __init__(self):
self.create_connection()
self.create_table()
def create_connection(self):
self.conn = mysql.connector.connect(
host='localhost',
user='root',
passwd='sTUDIO987',
database='lecturesinparis_db'
)
self.curr = self.conn.cursor()
def create_table(self):
self.curr.execute("""DROP TABLE IF EXISTS mdl""")
self.curr.execute("""create table mdl(
title text,
location text,
startdatetime text,
lenght text,
description text,
)""")
def process_item(self, item, spider):
self.store_db(item)
return item
def store_db(self, item):
self.curr.execute("""insert into mdl values (%s,%s,%s,%s,%s)""", (
item['title'][0],
item['location'][0],
item['startdatetime'][0],
item['lenght'][0],
item['description'][0]
))
self.conn.commit()
Advertisement
Answer
You have a coma too much,
use instead
def create_table(self):
self.curr.execute("""DROP TABLE IF EXISTS mdl""")
self.curr.execute("""create table mdl(
title text,
location text,
startdatetime text,
lenght text,
description text
)""")