Python %不支持的操作数类型:';长';和';unicode';

Python %不支持的操作数类型:';长';和';unicode';,python,Python,我正在试着做字符串替换 self.cursor.execute("select (1) from eScraperInterfaceApp_scrapeddata where productURL = '%s' limit 1") % URL 错误 %不支持的操作数类型:'long'和'unicode' productURL是unicode的,那么如何替换它。。。有人能帮我一下吗您的代码正在执行: self.cursor.execute("SQL template") % URL 应该是:

我正在试着做字符串替换

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

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

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

您的代码正在执行:

self.cursor.execute("SQL template") % URL
应该是:

self.cursor.execute("SQL template" % 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元组)。

我认为首选方式应改为正确方式,前一种方式仍然标记为不正确。请注意,字符串的%格式不推荐使用。如果使用Python2.6或更高版本,首选的方法是使用
.format()
。我相信通常的语法是对未知参数使用
(不过这可能是DBAPI特有的)。
cursor.execute(
    "select (1) from eScraperInterfaceApp_scrapeddata where productURL = %s limit 1;",
     (URL,))