python mysql插入

2024-10-02 18:14:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在用python开发一个scraper,它从RSS提要中收集紧急服务通告。在我想向MySQL数据库添加公告的时候,我遇到了一个奇怪的错误。我找不到任何关于它的东西。关于这一部分:

    sql = "INSERT INTO meldingen(melding_title, melding_description, melding_category, melding_region, melding_location, melding_zipcode, melding_emergency, melding_date) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"
        uniqueRows.append(hash)
        #print uniqueRows


        # Execute the SQL command
        cursor.execute(sql,(str(title), str(description), str(category), str(region), str(location), str(zipcode), str(emergency), date.strftime('%Y-%m-%d %H:%M:%S')))
        # Commit your changes in the database
        conn.commit()

我得到了以下错误:

Traceback (most recent call last): File "C:/Users/Nicky/Documents/alarmeringen/P2000Scraper.py", line 83, in cursor.execute(sql,(str(title), str(description), str(category), str(region), str(location), str(zipcode), str(emergency), date.strftime('%Y-%m-%d %H:%M:%S'))) File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 220, in execute self.errorhandler(self, exc, value) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorvalue _mysql_exceptions.ProgrammingError: (1064, "Erreur de syntaxe pr\xe8s de 'p 2 stank soort lucht: koolmonoxide/co vk: 6 troelstrakade dhg 7630'', ''Brandwe' \xe0 la ligne 1")

我不明白这个错误。如果有人能帮忙那就太好了。 谢谢。在


Tags: inexecutesqldatetitle错误locationdescription
2条回答

使用Python驱动程序时不要引用占位符。它会为你添加引号。'%s'在每种情况下都应该是%s。在

有关详细信息,请参见the documentation。在

您的SQL查询中有语法错误。它甚至在你提供的错误中这样说。在

"INSERT INTO meldingen(melding_title, ....)应该是"INSERT INTO meldingen (melding_title, .....)(注意表名和括号之间的空格)。在

相关问题 更多 >