【发布时间】:2017-05-05 12:03:25
【问题描述】:
我正在尝试将希伯来字母插入我的数据库 (mysql) 使用 request.form['some_input']
这是我的 init.py 的代码:
if request.method == "POST":
malfunction_opener = request.form['MalfunctionOpener']
malfunction_handler = request.form['malfunctionHandler']
system = request.form['system']
unit = request.form['unit']
我知道发生的事情是对我获得的信息进行编码/解码,但我无法做到这一点。 我也尝试过get_data,将其编码为utf-8,获取它的字符串值但没有成功。
在许多其他帖子中,我看到需要放置:
# -*- coding: utf-8 -*-
我也这样做了。
当我尝试插入任何希伯来字母时,我一直收到此错误,例如:“שלום”
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)
也许它是相关的,但我遇到的另一个问题是,当我将希伯来字母直接插入 mysql 并将其导入我的网站时,我得到了这些问号。 (但在 mysql 中我可以看到希伯来字母)。
谢谢!
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),))
我发现了我的第一个问题 - 我试图获取 len(str(request.form['MalfunctionOpener'])) ,它提供了这个乱码 ×©×œ×•× 并给了我一个错误
我还尝试输入我的数据库 repr(request.form['MalfunctionOpener']),它给了我这种类型的 u'\u203e\u203e\u203e\u203e\u203e\u203e\u203e\ u203e \u203e\u203e\u203e\' (只是一个例子)但我不知道如何从数据库中获取它并将其更改为希伯来语。
【问题讨论】:
标签: python mysql unicode flask