本來打算創建一個測試表進行一個簡單的實驗,發現創建完python_test表后插入數據后,select發現所有中文都變成問號了,這一看就是出現了亂碼
MariaDB [lhc]> create table python_test( -> id int(11), -> name varchar(255), -> class_time int(11), -> PRIMARY KEY(id) -> ); Query OK, 0 rows affected (0.03 sec) MariaDB [lhc]>
/*插入數據*/
INSERT INTO python_test(id,name,class_time) value(1,'字典',3); INSERT INTO python_test(id,name,class_time) value(2,'列表',2); INSERT INTO python_test(id,name,class_time) value(3,'函數',5); INSERT INTO python_test(id,name,class_time) value(4,'裝飾器',2); INSERT INTO python_test(id,name,class_time) value(5,'迭代器',2);
/*查看數據*/ MariaDB [lhc]> select * from python_test; +----+-----------+------------+
| id | name | class_time |
+----+-----------+------------+
| 1 | ?? | 3 |
| 2 | ?? | 2 |
| 3 | ?? | 5 |
| 4 | ??? | 2 |
| 5 | ??? | 2 |
+----+-----------+------------+
5 rows in set (0.00 sec)
首先查看下MariaDB的默認編碼格式
MariaDB [lhc]> show variables like "character_set_%" -> ; +--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
發現上面的數據庫的字符編碼不是為utf8
/*修改為utf8編碼格式*/ MariaDB [lhc]> set character_set_database=utf8; Query OK, 0 rows affected (0.00 sec) /*查看*/ MariaDB [lhc]> show variables like "character_set_%"; +--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
查看數據還是問號,說明問題並不出現在數據庫中,查看一下連接編碼
MariaDB [lhc]> show variables like "collation_%"; +----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
可以發現問題就是出現在連接層。
解決方法
/*1*/
SET NAMES 'utf8'; /* 它相當於下面的三句指令: SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8; */
/*2*/
/*創建數據庫的時候指定編碼格式*/ MariaDB [lhc]> create database python_test character set utf8; /*3*/
/*創建表的時候指定編碼格式*/ MariaDB [lhc]> create table python_test( -> id int(11), -> name varchar(50) character set utf8, -> class_time int(11), -> PRIMARY KEY(id) -> )DEFAULT CHARSET=utf8;
如果已經創建完成的表或者數據庫通過ALTER進行修改
/*修改某一個數據庫*/ MariaDB [lhc]> ALTER database python_test character set utf8; /*修改某一個表*/ MariaDB [lhc]> ALTER table python_test character set utf8;
再次查看原亂碼表就不亂碼了
MariaDB [lhc]> select * from python_test; +----+-----------+------------+
| id | name | class_time |
+----+-----------+------------+
| 1 | 字典 | 3 |
| 2 | 列表 | 2 |
| 3 | 函數 | 5 |
| 4 | 裝飾器 | 2 |
| 5 | 迭代器 | 2 |
+----+-----------+------------+