NWOWN
NWOWN

Reputation: 407

TypeError: cannot concatenate 'str' and 'float' objects in Flask with MySQL

I make python code work with MySQL and Flask.

If a user input a location name and radius in web, python code search the information about the location and others location that within the radius in MySQL.

Here is my code

'names' is location name entered by the user.
'ron' is the radius entered by the user.

 @app.route('/index', methods=["GET","POST"])
 def search_location(names, ron):
    row2=[]
    dis=ron/100
    cur = mysql.connect().cursor()
    cur.execute("select * from map_info where loc_name = '"+names+"'")
    info_db = cur.fetchall()

    cur.execute("select mapY, mapX from map_info where loc_name = '"+names+"'")
         for row in cur:
             print row
    row2.append(float(row[0]))
    row2.append(float(row[1]))
    row2.append(int(dis))

    cur.execute("select loc_name, mapY, mapX ( 6371 * acos( cos( radians("+row2[0]+ ") * cos( radians( mapY ) ) * cos( radians( mapX )- radians("+row2[1]+"))+ sin( radians("+row2[0]+") ) * sin( radians( mapY ) ) ) ) as distance FROM map_info HAVING distance < "+row2[2])

return render_template('index.html', results=info_db, rons=ron)

But 'TypeError: cannot concatenate 'str' and 'float' objects' is occurs in the last SQL statement that search the location within radius.

How can I fix it?
I would appreciate your advice.

Upvotes: 0

Views: 441

Answers (1)

Mangat Rai Modi
Mangat Rai Modi

Reputation: 5706

row2[] is Float but you are trying to concatenate it with a String. Python has a strict type checking (no implicit type conversions). Solution is to change row2[index] to string. Try this:-

cur.execute("select loc_name, mapY, mapX ( 6371 * acos( cos( radians("+str(row2[0])+ ") * cos( radians( mapY ) ) * cos( radians( mapX )- radians("+str(row2[1])+"))+ sin( radians("+str(row2[0])+") ) * sin( radians( mapY ) ) ) ) as distance FROM map_info HAVING distance < "+str(row2[2]))

Upvotes: 2

Related Questions