0

I'm new to python and I've got an error that i tried to solve by myself for couple of hours with no result. (I use Pycharm & SQLite).Here's my code:

Id = cur.execute("select id from abonnement where libelle=%s and prix=%s and estPackGar=%s ;" (abonnement.libelle, abonnement.prix, abonnement.estPackGar))

And this is what I get for that line:

TypeError: 'str' object is not callable

I do not have a clue about what is causing that. Any idea please?

Ron Smith
  • 3,241
  • 1
  • 13
  • 16

1 Answers1

1

You are missing a %, so the formatting doesn't get applied as you would like. Instead you need:

Id = cur.execute("select id from abonnement where libelle=%s and prix=%s and estPackGar=%s ;" % (abonnement.libelle, abonnement.prix, abonnement.estPackGar))

As for why you get the error you get: Essentially your error message results from having parenthesis directly after the string, a simplified example would be

"abc %s"(variable)

In which case python tries to call "abc %s" as though it were a function which fails because it's actually a string. This is why you get the "str is not callable" error. If however you use the string formatting correctly then you get the desired results:

"abc %s"%(variable)

I'd suggest using .format instead however, see this PEP: https://www.python.org/dev/peps/pep-3101/ and this question for more details about that: Python string formatting: % vs. .format

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • @user3836861, glad this helped. I know the first time I ran into this error I was stumped because it seemingly had nothing to do with the code I was using. – shuttle87 Jan 16 '15 at 18:47