%不支持的操作数类型:'long'和'unicode'

vai*_*ain 2 python

我正在尝试更换字符串

self.cursor.execute("select (1) from eScraperInterfaceApp_scrapeddata where productURL = '%s' limit 1") % URL
Run Code Online (Sandbox Code Playgroud)

错误

%不支持的操作数类型:'long'和'unicode'

productURL是unicode所以如何更换它...有人可以帮助我

fal*_*tru 5

你的代码正在做:

self.cursor.execute("SQL template") % URL
Run Code Online (Sandbox Code Playgroud)

它应该是:

self.cursor.execute("SQL template" % URL)
Run Code Online (Sandbox Code Playgroud)

改变位置):

self.cursor.execute("select (1) from eScraperInterfaceApp_scrapeddata where productURL = '%s' limit 1" % URL)
Run Code Online (Sandbox Code Playgroud)

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

self.cursor.execute("select (1) from eScraperInterfaceApp_scrapeddata where productURL = %s limit 1", (URL,))
Run Code Online (Sandbox Code Playgroud)