%的操作数类型不受支持:“long”和“unicode”

2024-06-26 14:20:57 发布

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

我在试着换线

self.cursor.execute("select (1) from eScraperInterfaceApp_scrapeddata where productURL = '%s' limit 1") % URL

错误

Unsupported operand type(s) for %: 'long' and 'unicode'

productURL是unicode,那么我如何替换它。。。有人能帮帮我吗


Tags: fromselfurlexecute错误unicodewhereselect
2条回答

您的代码正在执行以下操作:

self.cursor.execute("SQL template") % URL

它应该是:

^{pr2}$

更改)的位置:

self.cursor.execute("select (1) from eScraperInterfaceApp_scrapeddata where productURL = '%s' limit 1" % URL)

实际上更正确的方法是使用查询参数(防止SQL注入):

self.cursor.execute("select (1) from eScraperInterfaceApp_scrapeddata where productURL = %s limit 1", (URL,))

您没有指定使用什么数据库(和DBAPI驱动程序),但是pep249要求DBAPI支持参数替换,因此使用它代替Python字符串格式会更好。在

cursor.execute(
    "select (1) from eScraperInterfaceApp_scrapeddata where productURL = %s limit 1;",
     (URL,))

(注意参数占位符和参数作为1元组缺少引号)。在

相关问题 更多 >