A
A
alk2015-05-15 14:17:22
Django
alk, 2015-05-15 14:17:22

Why data is not saved in MySQL DB?

Good day to all!
I have this problem:
There is a model

class Test(models.Model):
    """
    The model of the test
    """

    # The name of the test
    name = models.CharField(max_length=500)
    # The description of test
    description = models.TextField(blank=True)
    # The test
    test = models.BinaryField(blank=True)
    # The author of the test
    author = models.ForeignKey(settings.AUTH_USER_MODEL)
    # The category of the test
    category = models.ForeignKey(Category)
    # date and time of create test
    date_and_time = models.DateTimeField(default=timezone.now())
    # How many users complete this test.
    rating = models.IntegerField(default=0)
    # Public or not public test
    is_public = models.BooleanField(default=True)

    def __str__(self):
        return self.name

At attempt I create class Test, I fill it, and I save. As a result, I get an error:
Exception Type: UnicodeDecodeError
Exception Value: 'utf-8' codec can't decode byte 0x80 in position 74: invalid start byte
Stack trace:
(django17)11:45 ~/exam_project (master)$ python3 populate_exam.py                                                                                                                                                
Starting Exam population script...
Traceback (most recent call last):
  File "populate_exam.py", line 214, in <module>
    populate()
  File "populate_exam.py", line 40, in populate
    category1
  File "populate_exam.py", line 208, in add_test
    )[0]
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/query.py", line 422, in get_or_create
    return self.get(**lookup), False
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/query.py", line 351, in get
    num = len(clone)
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/query.py", line 122, in __len__
    self._fetch_all()
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/django/db/backends/utils.py", line 85, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/mysql/connector/django/base.py", line 377, in last_executed_query
    return cursor.statement
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/mysql/connector/django/base.py", line 153, in __getattr__
    return getattr(self.cursor, attr)
  File "/home/alkutepov/.virtualenvs/django17/lib/python3.4/site-packages/mysql/connector/cursor.py", line 858, in statement
    return self._executed.strip().decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 265: invalid start byte
(django17)11:45 ~/exam_project (master)$

The error is modeled only in MySql, everything works fine in sqlite.
I'm using Django 1.7.7 and python3.4 I
suspect that I can't write data to a field of type BinaryField
Who faced a similar problem? How to fix?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
marazmiki, 2015-05-15
@Alexey_Kutepov

Go, the table in mysql is created in LATIN1?

R
Roman, 2015-05-15
@skipirich

If you have access to the console or some thread to manage databases like phpmyadmin, then try to run these commands

ALTER ИМЯ_ТВОЕЙ_БД `database_utf8` CHARACTER SET 'utf8';
ALTER ИМЯ_ТВОЕЙ_БД `database_utf8` COLLATE 'utf8_general_ci';

After that, you need to convert the tables
SELECT CONCAT(  'ALTER TABLE `', t.`TABLE_SCHEMA` ,  '`.`', t.`TABLE_NAME` ,  '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' ) AS sqlcode
FROM  `information_schema`.`TABLES` t
WHERE 1
AND t.`TABLE_SCHEMA` =  'ИМЯ_ТВОЕЙ_БД'
ORDER BY 1
LIMIT 0 , 90

A
Artur Nurullin, 2015-05-15
@Splo1ter

Because it is better to use PostgreSQL, MySQL has long outlived its usefulness, it is chosen if they do not know how PostgreSQL works, PostgreSQL is practically MS SQL just for free.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question