MySQLdb 操作数据库时的中文问题
时间:2007-03-06 来源:panwei789
设置 MySQL 的 my.cnf 文件,在 [client]/[mysqld]部分都设置默认的字符集:
CODE:
[Copy to clipboard]
[client]
default-character-set = utf8
[mysqld]
default-character-set = utf8
default-character-set = utf8
[mysqld]
default-character-set = utf8
该设置需要重新启动 MySQL 服务器。启动之后在 mysql 命令行下可以用 'status' 命令查看:
CODE:
[Copy to clipboard]
mysql> status
--------------
mysql Ver 14.12 Distrib 5.0.26, for -netbsdelf (i386) using EditLine wrapper
Connection id: 16
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.0.26-log
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 1 hour 7 min 49 sec
--------------
mysql Ver 14.12 Distrib 5.0.26, for -netbsdelf (i386) using EditLine wrapper
Connection id: 16
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.0.26-log
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 1 hour 7 min 49 sec
* 在创建表的时候,加上 default character='utf8' 这样的语句:
CODE:
[Copy to clipboard]
CREATE TABLE IF NOT EXISTS table_name (key, key, key)
VALUES (value, value, value)
DEFAULT CHARACTER='utf8'
VALUES (value, value, value)
DEFAULT CHARACTER='utf8'
* 在执行 MYSQL 语句时先执行 'SET NAMES 'utf8'' 语句:
CODE:
[Copy to clipboard]
SET NAMES 'utf8'
这样基本就能搞定中文问题了。
相关阅读 更多 +