@
@
@nixbox2019-11-14 18:59:05
PHP
@nixbox, 2019-11-14 18:59:05

How to fix encoding issue (UnicodeEncodeError: 'latin-1' codec can't encode characters..) when interacting with mysql?

Hello!
The problem (at least) with entering information into the Mysql database.
I am getting an error:

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)

I enter data through a web form, not through the console.
Everything was fine on the local, the error began to appear when transferring to prod.
Database encoding - utf8mb4
Flask (Sqlalchemy) database connection encoding - utf8mb4
Python version 3.7.4
OS - Ubuntu 18
Thank you!

Answer the question

In order to leave comments, you need to log in

6 answer(s)
H
Herman Martin, 2018-10-31
@dklight

if you leave these lines, then 0 or $ad_id is written,
what's wrong here??

The code is certainly good, but what do you want to get as an output?

M
MIK Ek, 2018-10-31
@MIKEk8

0) If you yourself can’t understand what’s going on in the code, it’s better to hire a programmer. And don't go there yourself.
1) Your lines are incorrectly specified, these are 75 and 97 and not 83 and .
2) Line 97 does not affect anything. it starts with // which means that this is a comment and it is not executed
3) This comment says that $pending = 0 by default. If you delete it on line 75, then inside the add function it should be 0 by default. BUT the comment is not the code. Most likely you have changed the default value. See function add on the messages class in the "classes/messages.php" file.
4) next, you need to watch what happens in the above method (add) when $pending 1 or 0 is received.
There are no conditions associated with incorrect behavior in this code, and you most likely do not need to remove $pending = 0

N
nixbox, 2019-11-18
_

So, the problem was not in the database and Python,
but in the language of the system (VPS / Ubuntu 18)
in which the Russian language was not installed at all.
Check system encoding:

import sys
import locale

print(sys.getfilesystemencoding())
print(locale.getpreferredencoding())

I just had ISO 8859-1 (Latin-1) on my VPS.
The solution is to add the locale to the RU system.

C
CopsisOnFire, 2021-05-18
@CopsisOnFire

Yesterday I caught the same error on a freshly bought server with ubuntu 20. I launched a cart bot. Helped dpkg-reconfigure locales there we select all EN.UTF-8 and all Ru-RU. Then I manually wrote LANG=ru_RU.UTF and it all worked.

L
lubezniy, 2019-11-14
@lubezniy

In a specific field put something like COLLATE utf8_general_ci

L
Lazy @BojackHorseman MySQL, 2019-11-14
Tag

collate can be assigned to schema, table or field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question