I have a web application written using Flask, SQLAlchemy, and MySQL. When I get strings submitted via form (e.g. request.form['Username']) they are submitted through SQLAlchemy and ultimately to MySQL as Unicode strings. My database is using the latin-1 charset, so I’m really looking to use latin-1 throughout. Is there a way to force Flask or SQLAlchemy to always use/convert to latin-1 without manually doing string.encode('latin-1')?
As an example of how this causes problems, when I do a SELECT...WHERE Name=:name and provide request.form['Name'] as the bound parameter, it’s going through as u'Some Name' and therefore not finding any matches.
You can set the encoding of the SQLAlchemy connection to MySQL in the connection parameters:
See the SQLAlchemy documentation on MySQL, unicode section.