How to insert and select Greek words in mysqldb

I created a DB and I use Mysqldb.
I want to insert and select the type of data characters, and that the characters are in Greek.
I set the sorting of my database to utf-8_general_ci, but when I insert the Greek word, I see something strange, then I try to select the rows of this table through python, and the result will be this (1,??????) .

Can anybody help me? thanks

+4
source share
2 answers

Perhaps the problem is with your encoding, be sure to install utf8 in python and try installing db:

 set character_set_database = utf8; set character_set_server = utf8; set character_set_system = utf8; set collation_database = utf8_general_ci; set collation_server = utf8_general_ci; set names utf8; 

Or using the Greek character set :-)

Python:

 db = MySQLdb.connect(host="localhost", use_unicode = True, charset = "utf8", user=username, passwd=password, db=database) 

Then, for output, you need to specify the line: Unicode:

 print (yourStringFromDatabase).encode("iso-8859-1") 
+2
source

Do you use Unicode from Python?

The secret ingredient is to add charset = "utf8" to your connection parameters and use_unicode = True. Source

 db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS, db=DB_NAME, charset="utf8", use_unicode=True) 

Note that going further, while in Python 3, Unicode strings are the norm, in earlier versions, unicode strings and strings are different things.

+2
source

Source: https://habr.com/ru/post/1394940/


All Articles