乱码
解决方法
1.查看默认的编码格式:
mysql> show variables like "%char%";
Variable_name | Value |+--------------------------+---------------+| character_set_client | gbk || character_set_connection | gbk || character_set_database | utf8 || character_set_filesystem | binary || character_set_results | gbk || character_set_server | utf8 || character_set_system | utf8 |
2.执行SET NAMES utf8的效果等同于同时设定如下:
SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';
2.查看test数据库的编码格式:mysql> show create database test;
+------------+------------------------------------------------------------------------------------------------+
| Database | Create Database |+------------+------------------------------------------------------------------------------------------------+
| test | CREATE DATABASEtest
/*!40100 DEFAULT CHARACTER SET gbk */ |+------------+------------------------------------------------------------------------------------------------+
3.查看yjdb数据库的编码格式:mysql> show create table yjdb;
| yjdb |
CREATE TABLEyjdb
(sn
int(5) NOT NULL AUTO_INCREMENT,
type
varchar(10) NOT NULL,
brc
varchar(6) NOT NULL,
teller
int(6) NOT NULL,
telname
varchar(10) NOT NULL,
date
int(10) NOT NULL,
count
int(6) NOT NULL,
back
int(10) NOT NULL,
PRIMARY KEY (sn
),
UNIQUE KEYsn
(sn
),
UNIQUE KEY sn_2
(sn
))
ENGINE=MyISAM AUTO_INCREMENT=1826 DEFAULT CHARSET=gbk ROW_FORMAT=DYNAMIC |
避免导入数据有中文乱码的问题
1.将数据编码格式保存为utf-8设置默认编码为utf8:
set names utf8;设置数据库db_name默认为utf8:
ALTER DATABASE db_name
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
设置表tb_name默认编码为utf8:
ALTER TABLE tb_name
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;导入:LOAD DATA LOCAL INFILE 'C:\utf8.txt' INTO TABLE yjdb;
2:将数据编码格式保存为ansi(即GBK或GB2312)设置默认编码为gbk:
set names gbk;
设置数据库db_name默认编码为gbk:
ALTER DATABASE db_name
DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
设置表tb_name默认编码为gbk:
ALTER TABLE tb_name
DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;导入:LOAD DATA LOCAL INFILE 'C:\gbk.txt' INTO TABLE yjdb;
注:1.UTF8不要导入gbk,gbk不要导入UTF8;2.dos下不支持UTF8的显示;