0

I'm trying to insert hebrew letters into my database (mysql) using request.form['some_input']

This is the code for my init.py:

    if request.method == "POST":
        malfunction_opener = request.form['MalfunctionOpener']
        malfunction_handler = request.form['malfunctionHandler']
        system = request.form['system']
        unit = request.form['unit']

I understand that what happening is about encoding/decoding the information I get, but I can't make this work. I've also tried get_data, encoding it to utf-8, getting the string value of it but without success.

In many other posts I saw that it's required to put :

# -*- coding: utf-8 -*-

which I also did.

all the time I keep getting this error when I'm trying to insert any hebrew letters, for example: "שלום"

2017-05-02 17:35:23,930 - Rotating Log - ERROR - 'ascii' codec can't encode 
characters in position 0-3: ordinal not in range(128)
2017-05-02 17:35:23,931 - Rotating Log - ERROR - 
request.form['malfunctionDescription'] value is: שלו×
2017-05-02 17:35:23,932 - Rotating Log - ERROR - 
Traceback (most recent call last):
File "/var/www/WebApp/WebApp/__init__.py", line 166, in new_malfunction
thwart(malfunction_description), thwart(malfunction_solution), 
thwart(malfunction_summary),))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: 
ordinal not in range(128)

and maybe it's related but another problem I have is when I'm inserting hebrew letters directly to mysql and import it the my web I get these questions mark. (but in mysql I can see the hebrew letters).

Thanks!

<---------- EDIT WITH EXTRA CODE ------------>

        if len(str(request.form['malfunctionDescription'])) <= 0:
            flash("You must enter a description.")
            return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)
        else:
            malfunction_description = request.form['malfunctionDescription']

        if status == 0 and len(str(request.form['malfunctionSolution'])) <= 0:
            flash("You must enter a solution.")
            return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)
        else:
            malfunction_solution = request.form['malfunctionSolution']

        if status == 0 and len(str(request.form['malfunctionSummary'])) <= 0:
            flash("You must enter a summary.")
            return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)
        else:
            malfunction_summary = request.form['malfunctionSummary']

        x = c.execute("SELECT * FROM malfunctions WHERE malfunctionDescription = (%s)",
                      (malfunction_description,))

        if int(x) > 0:
            flash("You have already enter this malfunction")
            return render_template("new-malfunction.html", contacts=contacts, systems=systems, units=units)

        else:
            c.execute(
                "INSERT INTO malfunctions (status, openingTime, closingTime, malfunctionOpener, malfunctionHandler, system, unit, rank, malfunctionDescription, malfunctionSolution, malfunctionSummary) VALUES (%s,%s,%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                (thwart(status), thwart(opening_date), thwart(closing_date), thwart(malfunction_opener),
                 thwart(malfunction_handler), thwart(system), thwart(unit), thwart(rank),
                 thwart(malfunction_description), thwart(malfunction_solution), thwart(malfunction_summary),))

<---------------- UPDATES ------------>

  • I found out my first problem - I've tried to get the len(str(request.form['MalfunctionOpener'])) which provided this gibberish ×©×œ×•× and gave me an error

  • I've also tried to enter to my database repr(request.form['MalfunctionOpener']) which give me this type of u'\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e \u203e\u203e\u203e\' (just an example) but I don't have any idea how to get this from the database and change it to hebrew.

Elon Salfati
  • 1,537
  • 6
  • 23
  • 46

1 Answers1

0

×©×œ×•× is "Mojibake" for שלו See Trouble with utf8 characters; what I see is not what I stored

\u203e is Unicode for Ë; I suspect this is a red herring.

More notes on python.

Community
  • 1
  • 1
Rick James
  • 135,179
  • 13
  • 127
  • 222