0

I am trying to make a simple program using flask + mysql. I just receive two arguments in a POST and write to the database:

@app.route('/upd',methods=['POST'])
def upd():
   _lat = request.form['lat']
   _lng = request.form['lng']

   cur = mysql.connection.cursor()
   cur.execute('insert into locations(lat,lng) values (?,?)',(_lat,_lng,))
   cur.commit()

   return jsonify(request.form)

The problem is that when i try to post the data from the client , i am getting:

ProgrammingError: not all arguments converted during string formatting
127.0.0.1 - - [28/Apr/2018 23:46:14] "POST /upd HTTP/1.1" 500 -

If i comment the SQL statement, the client will receive the output of jsonify which looks correct:

{
"lat": "3.2001",
"lng": "-11.45465"
}
1
  • 1
    MySQL uses printf style placeholders for queries, so you need to replace ? ? with %s %s. Commented Apr 29, 2018 at 3:04

1 Answer 1

1

You need to specify variables using %s or %(name)s parameter style, according to the docs:

The parameters found in the tuple or dictionary params are bound to the variables in the operation. Specify variables using %s or %(name)s parameter style (that is, using format or pyformat style).

cur.execute('insert into locations(lat,lng) values (%s,%s)' % (_lat,_lng,))

should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.