SQLite/python: 'str' 对象不可调用错误
SQLite/python: 'str' object is not callable error
我是 python 的新手,我遇到了一个错误,我试图自己解决几个小时但没有结果。 (我使用 Pycharm & SQLite)。这是我的代码:
Id = cur.execute("select id from abonnement where libelle=%s and prix=%s and estPackGar=%s ;" (abonnement.libelle, abonnement.prix, abonnement.estPackGar))
这就是我得到的那一行:
TypeError: 'str' object is not callable
我不知道是什么原因造成的。有什么想法吗?
您缺少一个 %
,因此没有按照您的意愿应用格式。相反,您需要:
Id = cur.execute("select id from abonnement where libelle=%s and prix=%s and estPackGar=%s ;" % (abonnement.libelle, abonnement.prix, abonnement.estPackGar))
至于为什么会出现错误:
本质上,您的错误消息是由于字符串后直接有括号引起的,一个简化的示例是
"abc %s"(variable)
在这种情况下,python 尝试调用 "abc %s"
,就好像它是一个失败的函数,因为它实际上是一个字符串。这就是您收到 "str is not callable" 错误的原因。但是,如果您正确使用字符串格式,那么您将获得所需的结果:
"abc %s"%(variable)
我建议改用 .format
,但是请参阅此 PEP:https://www.python.org/dev/peps/pep-3101/ and this question for more details about that: Python string formatting: % vs. .format
我是 python 的新手,我遇到了一个错误,我试图自己解决几个小时但没有结果。 (我使用 Pycharm & SQLite)。这是我的代码:
Id = cur.execute("select id from abonnement where libelle=%s and prix=%s and estPackGar=%s ;" (abonnement.libelle, abonnement.prix, abonnement.estPackGar))
这就是我得到的那一行:
TypeError: 'str' object is not callable
我不知道是什么原因造成的。有什么想法吗?
您缺少一个 %
,因此没有按照您的意愿应用格式。相反,您需要:
Id = cur.execute("select id from abonnement where libelle=%s and prix=%s and estPackGar=%s ;" % (abonnement.libelle, abonnement.prix, abonnement.estPackGar))
至于为什么会出现错误: 本质上,您的错误消息是由于字符串后直接有括号引起的,一个简化的示例是
"abc %s"(variable)
在这种情况下,python 尝试调用 "abc %s"
,就好像它是一个失败的函数,因为它实际上是一个字符串。这就是您收到 "str is not callable" 错误的原因。但是,如果您正确使用字符串格式,那么您将获得所需的结果:
"abc %s"%(variable)
我建议改用 .format
,但是请参阅此 PEP:https://www.python.org/dev/peps/pep-3101/ and this question for more details about that: Python string formatting: % vs. .format