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??

5
  • 3
    Post the traceback, don't just say "I keep getting a syntax error". Commented Nov 29, 2013 at 7:17
  • 2
    Are you sure item['name'] and item['content'] are strings and not, say, tuples Commented Nov 29, 2013 at 7:31
  • Pretty print or inspect in any other way your "item" object, it might not be exactly what you think... Commented Nov 29, 2013 at 7:32
  • @RedBaron Thanks man, I don't know how i slipped that. Commented Nov 29, 2013 at 7: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. Commented Nov 29, 2013 at 8:03

1 Answer 1

-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']
                    )
    )
Sign up to request clarification or add additional context in comments.

2 Comments

Please check your facts and stop posting wrong and ill-advised answers. And read this too: xkcd.com/327

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.