0

I have this query

        self.cursor.execute("""INSERT IGNORE INTO 
                        groups 
                        (
                            name,
                            builder_id,
                            content,city,
                            location,price,
                            target,
                            project_completion,
                            creator_id,verified,
                            live,
                            updater_id,
                            verify_id
                        ) 
                        VALUES 
                        (
                            %s,
                            %s,
                            %s,
                            %s,
                            %s,
                            %s,
                            %s,
                            '30',
                            '4',
                            '1',
                            '1',
                            '4',
                            '4'
                        );""", 
                       (
                            item['name'],
                            builder_id,
                            item['content'],
                            item['city'],
                            item['address'],
                            item['price'],
                            item['possession_date']
                        )
        )

I am scraping a few data and putting in into a mysql database using Scrapy. I am keep getting a syntax error

traceback

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 '),
                                18,
                                ("'friend' at line 19

18 is the builder_id and friend... is the start of content field any help??

kindall
  • 178,883
  • 35
  • 278
  • 309
goromlagche
  • 432
  • 1
  • 5
  • 12
  • 3
    Post the traceback, don't just say "I keep getting a syntax error". – abarnert Nov 29 '13 at 07:17
  • 2
    Are you sure `item['name']` and `item['content']` are strings and not, say, tuples – RedBaron Nov 29 '13 at 07:31
  • Pretty print or inspect in any other way your "item" object, it might not be exactly what you think... – bruno desthuilliers Nov 29 '13 at 07:32
  • @RedBaron Thanks man, I don't know how i slipped that. – goromlagche Nov 29 '13 at 07:53
  • The traceback tells you exactly what the problem is: one of the values is `("'friend'`—in other words, as @RedBaron said, one of your values is a tuple (because that's what the start of a tuple of strings looks like). And that's why you should give the traceback in your questions, instead of making people guess. – abarnert Nov 29 '13 at 08:03

1 Answers1

-1

The execute() method take only 1 single param.

cursor.execute('insert into a ("a", "b") values (1,2)')

So, there is not supposed to be ',' in your param. you could try this

self.cursor.execute("""INSERT IGNORE INTO 
                    groups 
                    (
                        name,
                        builder_id,
                        content,city,
                        location,price,
                        target,
                        project_completion,
                        creator_id,verified,
                        live,
                        updater_id,
                        verify_id
                    ) 
                    VALUES 
                    (
                        %s,
                        %s,
                        %s,
                        %s,
                        %s,
                        %s,
                        %s,
                        '30',
                        '4',
                        '1',
                        '1',
                        '4',
                        '4'
                    );""" % (
                        item['name'],
                        builder_id,
                        item['content'],
                        item['city'],
                        item['address'],
                        item['price'],
                        item['possession_date']
                    )
    )
Tiger Lu
  • 1
  • 3