MySQL 中的 ERROR 1366 (Mac)

在向一个表中插入中文数据时出错了,显示

ERROR 1366

查了一下,是编码格式的问题。解决方案如下:

首先,在终端里进入 MySQL 的命令行界面后,输入:

status

我的显示如下:

--------------
/usr/local/mysql/bin/mysql  Ver 14.14 Distrib 5.7.9, for osx10.9 (x86_64) using  EditLine wrapper

Connection id:      5
Current database:   db01
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.9 MySQL Community Server (GPL)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /tmp/mysql.sock
Uptime:         1 hour 37 min 0 sec

Threads: 1  Questions: 83  Slow queries: 0  Opens: 116  Flush tables: 1  Open tables: 102  Queries per second avg: 0.014
--------------

上面的

Db     characterset:    latin1

是问题所在。在终端中输入:

alter database db01 character set utf8;

成功之后再查看:

--------------
/usr/local/mysql/bin/mysql  Ver 14.14 Distrib 5.7.9, for osx10.9 (x86_64) using  EditLine wrapper

Connection id:      5
Current database:   db01
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.9 MySQL Community Server (GPL)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /tmp/mysql.sock
Uptime:         1 hour 37 min 0 sec

Threads: 1  Questions: 83  Slow queries: 0  Opens: 116  Flush tables: 1  Open tables: 102  Queries per second avg: 0.014
--------------

然而这只是改了这个数据库的默认,说的是你如果新建一张表,或者插入新的字符属性时它的编码会是 utf8 ,但是原来的字符属性还是 latin1 ,所以还是没有解决问题。

这个时候只用再把原来的属性修改一下就好了:

alter table book change title title varchar(50);

这个时候再插入新的数据时,title 属性的值就可以接受中文了。

你可能感兴趣的:(MySQL)