Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

python - MySQL fix Incorrect String Value with Django DB Settings

I'm having errors writing to MySQL from Django. Traceback below:

  File "/home/neil/web_app/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/home/neil/web_app/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 259, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1366, "Incorrect string value: '\xE2\x9D\x96 \x0AT...' for column 'body' at row 1")

I have attempted to set the encoding of the DB.

Settings.py:

DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.mysql',
                'NAME': 'citator_migration',
                'USER': ...,
                'PASSWORD': ...,
                'HOST': ...,
                'OPTIONS': {
                'init_command': "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; SET sql_mode='STRICT_TRANS_TABLES', character_set_connection='utf8mb4'"
                    },
            }
        }

Related: Setting Django/MySQL site to use UTF-8

Incorrect string value: 'xE2x80xAF(fo...' for column 'description' at row 1 Error: INSERT INTO my_table_name

Incorrect string value: 'xE2x80xAF(fo...' for column 'description' at row 1 Error: INSERT INTO my_table_name

What am I doing wrong? Is it possible to remedy with Django Settings or do I need to query MySQL directly to change the encoding? Should I be changing to utf8mb4 or latin1? The source of the data is a Postgres DB behind a Drupal API. It is fetched over http and then written to the DB with Django models


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...