I’m working on a project that involves a MySQL database and a Flask app in Python. I establish a connection to MySQL and query data using the cursor.execute method. However, I’m encountering strange characters in the rendered output on the webpage:
The Strange Characters
Upon debugging, I discovered that these characters originate from the database. Through research, I found that executing the following command resolves the issue:
SET NAMES CP850;
After running this command, the characters display correctly when querying the database through HeidiSQL. However, when rendering the webpage, the strange characters persist.
I also noticed that HeidiSQL will revert to showing the strange characters if the session is restarted, indicating that the command needs to be executed at the start of each session.
To address this, I attempted to execute the SET NAMES CP850; command in my Python script after creating the connection object, but the issue remains unresolved.
There is how I create the connection:
def intentar_conexión(nombre_usuario, contraseña):
try:
return mysql.connector.connect(
user=nombre_usuario,
password=contraseña,
host="localhost",
database="bienestar"
)
except mysql.connector.Error as err:
print("[Debug Message] MySQL Error: " + str(err))
return err.errno
I am using utf-8 on Python so there is no problem with the accentuated vowels and ñ on the script.
user25821995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.