For a very long time i was suffering form the Latin-1 encoding in My Django web application DB causing this error when trying to select a matching string using LIKE :
-- UnicodeEncodeError:'latin-1' codec can't encode character--
i've tried every solution from setting (charset='utf8') in the connection to applying cursor.execute("set names 'utf8'") before my actual query but nothing seams to work.
Until i came across this blog post: https://www.whitesmith.co/blog/latin1-to-utf8/ about the encoding problem and it is the same problem that i have because when i looked in phpMyAdmin a saw that by default my DB i Latin-1 encoding:
chatbot_brain=>utf8_general_ci
information_schema=>utf8_general_ci
Total: 2=> latin1_swedish_ci
So, the solution is to dump the DB and change the description in the schema file:
# Login into your future database host to create a new database with an UTF-8 charset
$ mysql -h FUTURE_HOST -u FUTURE_USER -p
mysql> CREATE DATABASE `FUTURE_DB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
# Flush the current database schema on the future host, replacing all CHARSET=latin1 occurrences along the way
mysqldump -h CURRENT_HOST -u CURRENT_USER -p CURRENT_DB --no-data --skip-set-charset --default-character-set=latin1 \
| sed 's/CHARSET=latin1/CHARSET=utf8/g' \
| mysql -h FUTURE_HOST -u FUTURE_USER -p FUTURE_DB --default-character-set=utf8
# Flush the current database data on the future host
mysqldump -h CURRENT_HOST -u CURRENT_USER -p --no-create-db --no-create-info --skip-set-charset --default-character-set=latin1 CURRENT_DB \
| mysql -h FUTURE_HOST -u FUTURE_USER -p FUTURE_DB --default-character-set=utf8
Now i know what is the problem and the solution, But my question is how i can applied to my Django project-- Do i have to use my computer terminal and SSH session or is there any application of that?
this is a sceen shot of my DB in phpmyAdmin:
Thank you
PS(I am using Django10 , Python3.5,Mysql,Webfaction sherd host)