create database kitty; Query OK, 1 row affected (0.00 ..."/>
您好,登錄后才能下訂單哦!
1)MySQL插入中文數(shù)據(jù)亂碼問題:
模擬亂碼現(xiàn)象: mysql> create database kitty; Query OK, 1 row affected (0.00 sec) mysql> show create database kitty\G; *************************** 1. row *************************** Database: kitty Create Database: CREATE DATABASE `kitty` /*!40100 DEFAULT CHARACTER SET utf8 */ 1 row in set (0.00 sec) ERROR: No query specified mysql> alter database kitty character set latin1; Query OK, 1 row affected (0.01 sec) mysql> flush privileges; mysql> use kitty; Database changed 創(chuàng)建teacher表(字符集為latin1): CREATE TABLE `teacher` ( `id` int(4) NOT NULL, `name` char(20) NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 插入兩條數(shù)據(jù),其中一行帶有中文字符 mysql> insert into teacher values(1,'wanlong','31','Server'),(2,'laomao','31','售后部'); Query OK, 2 rows affected, 1 warning (0.00 sec) Records: 2 Duplicates: 0 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 se 查看發(fā)現(xiàn)有亂碼出現(xiàn): mysql> select * from teacher; +----+---------+-----+--------+ | id | name | age | dept | +----+---------+-----+--------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | +----+---------+-----+--------+ 2 rows in set (0.00 sec)
2) 如何解決亂碼了
不亂碼的思想:建議中英文混合的環(huán)境,選擇utf-8
客戶端:
set names utf8(臨時(shí)生效)
修改my.cnf(永久生效)
[client]
default-character-set=utf8
服務(wù)端-庫-表-程序
服務(wù)端: [mysqld] default-character-set=utf8(適合5.1及以前的版本) character-set-server=utf8(適合5.5) 庫: 查看數(shù)據(jù)庫的編碼: mysql> show variables like'%char%'; +--------------------------+-------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /application/mysql-5.5.32/share/charsets/ | +--------------------------+-------------------------------------------+ 8 rows in set (0.00 sec) 表: 查看表的編碼: mysql> show create table teacher\G; *************************** 1. row *************************** Table: teacher Create Table: CREATE TABLE `teacher` ( `id` int(4) NOT NULL, `name` char(20) CHARACTER SET latin1 NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) CHARACTER SET latin1 DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified mysql> select * from teacher; +----+---------+-----+--------+ | id | name | age | dept | +----+---------+-----+--------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | +----+---------+-----+--------+ 3 rows in set (0.00 sec) mysql> show create table teacher\G; *************************** 1. row *************************** Table: teacher Create Table: CREATE TABLE `teacher` ( `id` int(4) NOT NULL, `name` char(20) CHARACTER SET latin1 NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified 修改字段的字符集: mysql> alter table `teacher` change `dept` `dept` varchar(16) CHARACTER SET utf8 NOT NULL; mysql> alter table `teacher` change `name` `name` char(20) CHARACTER SET utf8 NOT NULL; mysql> show full columns from teacher; +-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+-- | Field | Type | Collation | Null | Key | Default | Extra | Privileges | C +-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+-- | id | int(4) | NULL | NO | | NULL | | select,insert,update,references | | name | char(20) | utf8_general_ci | NO | | NULL | | select,insert,update,references | | age | tinyint(2) | NULL | NO | | 0 | | select,insert,update,references | | dept | varchar(16) | utf8_general_ci | NO | | NULL | | select,insert,update,references | +-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+-- 4 rows in set (0.00 sec) mysql> select * from teacher; +----+---------+-----+--------+ | id | name | age | dept | +----+---------+-----+--------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | +----+---------+-----+--------+ 3 rows in set (0.00 sec) 測試再次插入帶有中文字符的字段: mysql> insert into teacher values(4,'萬龍',30,'校園網(wǎng)'),(5,'知行',29,'華東院'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from teacher; +----+---------+-----+-----------+ | id | name | age | dept | +----+---------+-----+-----------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | | 4 | 萬龍 | 30 | 校園網(wǎng) | | 5 | 知行 | 29 | 華東院 | +----+---------+-----+-----------+ 5 rows in set (0.00 sec)
3)老數(shù)據(jù)仍舊是亂碼!
說明:
a)對于已有數(shù)據(jù)庫想修改字符集不能直接通過“alter database kitty character set *”或者“alter table tablename character set *”,這兩個(gè)命令都沒有更新已有記錄的字符集,而只是對新創(chuàng)建的表或記錄生效。
b)已經(jīng)有記錄的字符的調(diào)整,必須先將數(shù)據(jù)導(dǎo)出,經(jīng)過修改字符集后重新導(dǎo)入后才可完成
修改數(shù)據(jù)庫默認(rèn)編碼:
“alter database kitty character set *”
4)參考解決方法:
如何更改生產(chǎn)MySQL數(shù)據(jù)庫庫表的字符集
1、導(dǎo)出表結(jié)構(gòu) mysqldump -uroot -predhat12345 -S /data/3306/mysql.sock --default-character-set=latin1 -d kitty>kittytable.sql 說明:-d只導(dǎo)表結(jié)構(gòu) 2、編輯kittytable.sql,將lantin1改成utf8 可以用sed批量修改 3、確保數(shù)據(jù)庫不再更新,導(dǎo)出所有數(shù)據(jù) mysqldump -uroot -predhat12345 -S /data/3306/mysql.sock --quick --no-create-info --extended-insert --default-character-set=latin1 kitty>kittydata.sql 4、打開kittydata.sql,將set names latin1修改成set names utf8 5、刪除原有的庫表及數(shù)據(jù)(需要慎重) 6、建庫 create database kitty default charset utf8; 7、創(chuàng)建表,執(zhí)行kittytable.sql mysql -uroot -predhat12345 -S /data/3306/mysql.sock kitty<kittytable.sql 8、導(dǎo)入數(shù)據(jù) mysql -uroot -predhat12345 -S /data/3306/mysql.sock kitty<kittydata.sql
5)執(zhí)行SQL文件插入中文數(shù)據(jù)不亂碼實(shí)戰(zhàn):
a、將需要更新的sql語句放到文本文件中
mysql> system cat test.sql set names utf8; insert into teacher values (6,'張飛',45,'商務(wù)部');
b、通過source來調(diào)用sql文件
mysql> source test.sql Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.01 sec) mysql> select * from teacher; +----+---------+-----+-----------+ | id | name | age | dept | +----+---------+-----+-----------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | | 4 | 萬龍 | 30 | 校園網(wǎng) | | 5 | 知行 | 29 | 華東院 | | 6 | 張飛 | 45 | 商務(wù)部 | +----+---------+-----+-----------+ 6 rows in set (0.00 sec)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。