Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the MySQLdb module in python returns utf8 encoding or unicode in this case?

Using MySQLdb I connect to a database where everything is stored in the utf8 encoding.

If I do

cursor.execute("SET NAMES utf8")

and fetch some data from the database by another statement. Does that mean, that the strings in

cursor.execute("SELECT ...")
cursor.fetchall()

will be in unicode? Or do I have to turn them first by

mystr.decode("utf8")

to unicode?

like image 970
Aufwind Avatar asked Dec 28 '22 20:12

Aufwind


1 Answers

From the docs:

connect(parameters...)
...

use_unicode

If True, CHAR and VARCHAR and TEXT columns are returned as Unicode strings, using the configured character set. It is best to set the default encoding in the server configuration, or client configuration (read with read_default_file). If you change the character set after connecting (MySQL-4.1 and later), you'll need to put the correct character set name in connection.charset.

If False, text-like columns are returned as normal strings, but you can always write Unicode strings.

This must be a keyword parameter.

like image 97
Ignacio Vazquez-Abrams Avatar answered Dec 30 '22 11:12

Ignacio Vazquez-Abrams