我正在使用MySQLdb
python模块连接到数据库。我连接到它的方法如下:
import MySQLdb as mysql
mysql_connection = mysql.connect(user=user, passwd=password, db=db,
charset='utf8', use_unicode=True)
cursor = mysql_connection.cursor()
# error checking snip here
# (ommitted for brevity)
return (mysql_connection, cursor)
针对此连接,我正在执行包含utf-8
字符串(python中的unicode
个对象)的查询,如下所示:
[DEBUG] INSERT INTO Clients(clientid, login, pname, email) VALUES (304, 'sample_username', 'Φώτης Κ', 'example@example.com');
但是我发现在实际数据库中输入的数据是错误的,实际上是这样的:
??????? ????????
我确实已确认mysql
已设置为接受unicode字符串,因为我已手动执行包含utf-8字符的查询并且它们已成功。
SHOW VARIABLES LIKE "character_set%"
命令的结果如下:
mysql> SHOW VARIABLES LIKE "character_set%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
我的特定表的架构(至少对于相关列)是这样的:
DROP TABLE IF EXISTS `Clients`;
CREATE TABLE `Clients` (
...
`login` VARCHAR(200) CHARACTER SET utf8,
`pname` VARCHAR(255) CHARACTER SET utf8,
`email` VARCHAR(255) CHARACTER SET utf8,
...
);
此外,我的终端设置为$LC_ALL
和$LANG
设置为el_GR.utf8
。这可能有什么问题?
答案 0 :(得分:1)
唉...
与所有事情一样,在我自己尝试了一些之后,我找到了答案。似乎插入的所有内容实际上都存在,只是MySQL
客户端没有正确显示它。
正如您从字符集结果中看到的那样,我在帖子中发布了这个问题:
mysql> SHOW VARIABLES LIKE "character_set%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
character_set_results
设置为latin1
。执行SET character_set_results=utf8;
设法修复所有内容,并且按预期工作。
答案 1 :(得分:0)
尝试在连接之后执行下面的查询,在任何其他可能包含utf8字符之前执行:
SET NAMES utf8
<强>更新强>
您应该从程序中尝试SHOW VARIABLES LIKE "character_set%";
,以确保所有内容都设置为utf8。
正如你所说,只有SET NAMES
无效,try being more verbose:
SET NAMES utf8;
SET CHARACTER SET utf8;
SET character_set_connection=utf8;