在这种情况下,python中的MySQLdb模块是否返回utf8编码或unicode?

2024-04-25 16:54:48 发布

您现在位置:Python中文网/ 问答频道 /正文

使用MySQLdb,我连接到一个数据库,其中所有内容都存储在utf8编码中。

如果我做了

cursor.execute("SET NAMES utf8")

并通过另一条语句从数据库中获取一些数据。这是不是意味着

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

将使用unicode?或者我必须先把它们转过去

mystr.decode("utf8")

到unicode?


Tags: 数据数据库内容编码executenamesunicode语句
1条回答
网友
1楼 · 发布于 2024-04-25 16:54:48

来自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.

相关问题 更多 >