Sql代碼 收藏代碼 mysql> use varchar_vs_char;
Database changed
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| username | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql
能夠插入的數據:sql
Sql代碼 收藏代碼 mysql> use varchar_vs_char;
Database changed
mysql> select * from user;
+----+----------------------+
| id | username |
+----+----------------------+
| 1 | adminadmin |
| 2 | 我是中國人我是中國人 |
+----+----------------------+
2 rows in set (0.01 sec)數據庫
也就是說對於unicode來講一個英文字符和一箇中文字符是同樣大小的,並且varchar(10)中的10就是表示10個unicode碼!post
由於是處理中文的,因此把D:\Program Files\MySQL\MySQL Server 5.1\my.ini編碼設置成以下gbk:this
Sql代碼 收藏代碼 [client]編碼
port=3306命令行
[mysql]code
default-character-set=gbkserver
[mysqld]blog
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="D:/Program Files/MySQL/MySQL Server 5.1/"
#Path to the database root
datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/Data/"
default-character-set=gbk
default-storage-engine=INNODB
附註:
更新的數據中有中文,出現以下錯誤: OperationalError at /admin/blog/post/add/
(1366, "Incorrect string value: '\xE6\xB7\xB1\xE5\x85\xA5...' for column 'title' at row 1")
Request Method: POST Request URL: http://localhost:8000/admin/blog/post/add/ Exception Type: OperationalError Exception Value:
(1366, "Incorrect string value: '\xE6\xB7\xB1\xE5\x85\xA5...' for column 'title' at row 1")
Exception Location: C:\Python25\Lib\site-packages\MySQLdb\connections.py in defaulterrorhandler, line 35
相似這樣的錯誤,應該是數據庫表的charset和collation問題。嘗試把全部表的charset改成utf-8, collation改成utf8-unicode-ci。若是仍是不能解決,最好是重建數據庫,而後修改數據庫的屬性,選擇charset爲utf-8,collation爲utf8-unicode-ci。命令行:create database cc default charset utf8 collate utf8_unicode_ci; PS:我就是要重建數據庫才解決。