Problema com语法mysql

时间:2016-09-09 14:56:40

标签: python mysql syntax

我正在python中构建一个爬虫,它运行良好,但在某些时候我遇到以下语法错误:

_mysql_exceptions.ProgrammingError: (1064, '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 \'), 1, 1, 1, 1, \'1e222a6f2be4477d2d3e8a3f82e0b474.jpg\', \'{\\"inheritFrom\\":\\"1\\",\\\' at line 3')

与此错误相关的抓取工具的一部分是:

par = '''{"inheritFrom":"1","theme":"","num_leading_items":"2","num_leading_columns":"1","leadingImgSize":"Large","num_primary_items":"4","num_primary_columns":"2","primaryImgSize":"Medium","num_secondary_items":"4","num_secondary_columns":"1","secondaryImgSize":"Small","num_links":"4","num_links_columns":"1","linksImgSize":"XSmall","catCatalogMode":"0","catFeaturedItems":"1","catOrdering":"","catPagination":"2","catPaginationResults":"1","catTitle":"1","catTitleItemCounter":"1","catDescription":"1","catImage":"1","catFeedLink":"1","catFeedIcon":"1","subCategories":"1","subCatColumns":"2","subCatOrdering":"","subCatTitle":"1","subCatTitleItemCounter":"1","subCatDescription":"1","subCatImage":"1","itemImageXS":"","itemImageS":"","itemImageM":"","itemImageL":"","itemImageXL":"","catItemTitle":"1","catItemTitleLinked":"1","catItemFeaturedNotice":"0","catItemAuthor":"1","catItemDateCreated":"1","catItemRating":"0","catItemImage":"1","catItemIntroText":"1","catItemIntroTextWordLimit":"","catItemExtraFields":"0","catItemHits":"0","catItemCategory":"1","catItemTags":"1","catItemAttachments":"0","catItemAttachmentsCounter":"0","catItemVideo":"0","catItemVideoWidth":"","catItemVideoHeight":"","catItemAudioWidth":"","catItemAudioHeight":"","catItemVideoAutoPlay":"0","catItemImageGallery":"0","catItemDateModified":"0","catItemReadMore":"1","catItemCommentsAnchor":"1","catItemK2Plugins":"1","itemDateCreated":"1","itemTitle":"1","itemFeaturedNotice":"1","itemAuthor":"1","itemFontResizer":"1","itemPrintButton":"1","itemEmailButton":"1","itemSocialButton":"1","itemVideoAnchor":"1","itemImageGalleryAnchor":"1","itemCommentsAnchor":"1","itemRating":"1","itemImage":"1","itemImgSize":"Large","itemImageMainCaption":"1","itemImageMainCredits":"1","itemIntroText":"1","itemFullText":"1","itemExtraFields":"1","itemDateModified":"1","itemHits":"1","itemCategory":"1","itemTags":"1","itemAttachments":"1","itemAttachmentsCounter":"1","itemVideo":"1","itemVideoWidth":"","itemVideoHeight":"","itemAudioWidth":"","itemAudioHeight":"","itemVideoAutoPlay":"0","itemVideoCaption":"1","itemVideoCredits":"1","itemImageGallery":"1","itemNavigation":"1","itemComments":"1","itemTwitterButton":"1","itemFacebookButton":"1","itemGooglePlusOneButton":"1","itemAuthorBlock":"1","itemAuthorImage":"1","itemAuthorDescription":"1","itemAuthorURL":"1","itemAuthorEmail":"0","itemAuthorLatest":"1","itemAuthorLatestLimit":"5","itemRelated":"1","itemRelatedLimit":"5","itemRelatedTitle":"1","itemRelatedCategory":"0","itemRelatedImageSize":"0","itemRelatedIntrotext":"0","itemRelatedFulltext":"0","itemRelatedAuthor":"0","itemRelatedMedia":"0","itemRelatedImageGallery":"0","itemK2Plugins":"1","catMetaDesc":"","catMetaKey":"","catMetaRobots":"","catMetaAuthor":""}'''
            plugins = '''{"incptvk2multipleextrafieldgroups":["0"]}'''
            sql = '''INSERT INTO ybis8_k2_categories(`name`, `alias`, `description`,
                    `parent`, `extraFieldsGroup`, `published`, `access`, `ordering`, `image`,
                    `params`, `trash`, `plugins`, `language`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''

prefixo = binascii.hexlify(os.urandom(16))
args = (categoria_string, categoria_string, "", cid, 1, 1, 1, 1, prefixo + ".jpg", par, 0, plugins, "*")

cur.execute(sql, args)

如果你能帮助我,非常感谢你:D

1 个答案:

答案 0 :(得分:0)

我也遇到过多行sql查询作为python字符串的问题(例如,''' '''三重引号内的查询就像你的一样)。我认为这是因为涉及的空格(换行符,缩进等)在三重引号字符串中保留。一种解决方案是将您的查询作为一行 - 这不是一个很好的选择,因为它不是pythonic而且不可读。

第二个选项是在括号中将多个字符串组合在一起:

sql = ('INSERT INTO ybis8_k2_categories(`name`, `alias`, `description`, '
       '`parent`, `extraFieldsGroup`, `published`, `access`, `ordering`, `image`, '
       ' `params`, `trash`, `plugins`, `language`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ')

括号内的字符串在Python中自动连接。注意每个字符串末尾的空格。这与多行字符串一样易于阅读,但具有实际应用的优点:)

我知道我可能不应该回答这个问题,因为它既可以用英语,也可以在StackExchange站点上用于您的母语,但是您的问题在代码中看起来非常清楚,我之前已经解决了这个问题所以我想我会给你一个答案。