【问题标题】:MySqlDb python, syntax error, can not understandMySqlDb python,语法错误,看不懂
【发布时间】:2013-12-15 07:50:46
【问题描述】:

我有这个问题

        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']
                        )
        )

我正在使用 Scrapy 抓取一些数据并放入 mysql 数据库。 我不断收到语法错误

追溯

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 是builder_id 和friend...是内容字段的开始 有什么帮助吗??

【问题讨论】:

  • 发布回溯,不要只说“我不断收到语法错误”。
  • 你确定item['name']item['content'] 是字符串,而不是元组
  • 漂亮打印或以任何其他方式检查您的“项目”对象,它可能与您的想法不完全一样......
  • @RedBaron 谢谢老兄,我不知道我是怎么漏掉的。
  • 回溯告诉你问题出在哪里:其中一个值是("'friend'——换句话说,正如@RedBaron 所说,你的一个值是一个元组(因为那是一个字符串元组看起来像)。这就是为什么你应该在你的问题中提供追溯,而不是让人们猜测。

标签: python mysql syntax-error


【解决方案1】:

execute() 方法只需要 1 个参数。

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

因此,您的参数中不应该有“,”。你可以试试这个

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']
                    )
    )

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
相关资源
最近更新 更多