-3

i'm connecting my app to sql server and want to convert id which is in int type in str type but it gives error " 'str' object is not callable "

i have tried it in terminal and the program runs successfully but getting error while running main program and type of res[0] is <class 'int'>

def findquestion(str):
    resp=""
    mydb=mysqlc.connect(host="localhost",user="admin",passwd="admin",database="admin")
    mycursor = mydb.cursor()
    mycursor.execute("select qid,title from questions where title like %s",("%"+str+"%",))
    results = mycursor.fetchall()
    mydb.close()
    for res in results:
        id=str(res[0])
        resp+="<a href='/question/"+id+"'>"+res[1]+"</a><br>"
    return resp

getting error

  File "/opt/web-sites/app.py", line 150, in findquestion
    id=str(res[0])
TypeError: 'str' object is not callable

sorry for weak english

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Bhupesh lad
  • 400
  • 3
  • 15

2 Answers2

0

The error arises from redefining the built-in str() function. Simply rename your parameter and the error should resolve itself.

Alec
  • 8,529
  • 8
  • 37
  • 63
0

As pointed in the comments you have a problem with the scope of your variable str

def findquestion(_str):
    resp=""
    mydb=mysqlc.connect(host="localhost",user="admin",passwd="admin",database="admin")
    mycursor = mydb.cursor()
    mycursor.execute("select qid,title from questions where title like %s",("%"+_str+"%",))
    results = mycursor.fetchall()
    mydb.close()
    for res in results:
        id=str(res[0])
        resp+="<a href='/question/"+id+"'>"+res[1]+"</a><br>"
    return resp