

Convert All Default Characters of MySQL Databases & Tables To utf8mb4
source link: https://www.codeproject.com/Articles/5328564/Convert-Default-Characters-Collations-of-MySQL-Dat
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction
This article explains the simiple solution (written in C#) to convert the default characters to utf8mb4
for all MySQL databases and tables.
Background
utf8mb4
is the default character set started in MySQL 8. It has the best support for all the language characters of the world, including emoji characters etc. Thus, using utf8mb4 has the best compatible if your application uses variety of unicode characters. This article will be useful when you need to perform a batch conversion throughout all the databases and tables from old projects at once.
Using the code
You may add one of the following Nuget Package : MySQL connector for this to work:
The idea is basically get the list of databases, then loop through the database and get the tables. Perform a loop on all tables and check it's default character set, if it is not utf8mb4
, then convert it one by one.
First, obtain the list of database:
SELECT * FROM information_schema.SCHEMATA;
In C#,
using (MySqlConnection conn = new MySqlConnection(constr)) { using (MySqlCommand cmd = new MySqlCommand()) { conn.Open(); cmd.Connection = conn; DataTable dtDatabase = new DataTable(); cmd.CommandText = "SELECT * FROM information_schema.SCHEMATA;"; MySqlDataAdapter da1 = new MySqlDataAdapter(cmd); da1.Fill(dtDatabase); conn.Close(); } }
This will return a table that has the following column details:
SCHEMA_NAME
DEFAULT_CHARACTER_SET_NAME
DEFAULT_COLLATION_NAME
Here, character set can be checked. If it is not set to "utf8mb4", then it will be modified.
But first, the following databases are needed to be ignored, as they are MySQL readonly specific info:
information_schema
mysql
performance_schema
sys
The SQL statement for conversion of database character set:
ALTER DATABASE `{database name}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
In C#,
foreach (DataRow dr in dtDatabase.Rows) { string database = dr["SCHEMA_NAME"] + ""; // ignore switch (database) { case "information_schema": case "myql": case "performance_schema": case "sys": continue; } string db_charset = dr["DEFAULT_CHARACTER_SET_NAME"] + ""; string db_collation = dr["DEFAULT_COLLATION_NAME"] + ""; if (db_charset == "utf8mb4" && db_collation == "utf8mb4_general_ci") { // do nothing } else { cmd.CommandText = $"ALTER DATABASE `{database}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"; cmd.ExecuteNonQuery(); } }
Next, is to obtain the list of tables with the following SQL statement:
show table status;
In C#,
cmd.CommandText = $"use `{database}`"; cmd.ExecuteNonQuery(); cmd.CommandText = "show table status;"; DataTable dtTables = new DataTable(); MySqlDataAdapter da2 = new MySqlDataAdapter(cmd); da2.Fill(dtTables);
Then, loop through each table to obtain the following column value:
Name
= the table's nameCollation
= the collation character set
The SQL statement for conversion of table's character set:
ALTER TABLE `{tablename}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
In C#,
foreach (DataRow dr2 in dtTables.Rows) { string tablename = dr2["Name"] + ""; string tableCollation = dr2["Collation"] + ""; if (tableCollation != "utf8mb4_general_ci") { try { cmd.CommandText = $"ALTER TABLE `{tablename}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"; cmd.ExecuteNonQuery(); } catch (Exception ex) { // log the error } } }
History
2022-03-29: First release
Recommend
-
35
Today’s bug: I tried to store a UTF-8 string in a MariaDB “utf8”-encoded database, and Rails raised a bizarre error: Incorrect string value: ‘\xF0\x9F\x98\x83 <…’ for column ‘summary’ at row 1 Thi...
-
49
Today’s bug: I tried to store a UTF-8 string in a MariaDB “utf8”-encoded database, and Rails raised a bizarre error: This is a UTF-8 client and a UTF-8 server, in a UTF-8 database with a UTF-8…
-
42
今天的错误:我试图将一个UTF-8字符串存储在MariaDB“utf8”编码的数据库中,并且引发了一个奇怪的错误: Incorrect string value: ‘\xF0\x9F\x98\x83 <…’ for column ‘summary’ at row 1 这是UTF-8客户端和UTF-8服务...
-
16
问题说明 业务数据库实例的编码由 utf8 修改为 utf8mb4 后, java 业务插入表情符等宽字符(4 字节)的时候一直报错以下相关的错误: ### Cause:java.sql.SQLException:Incorrect string value:\xF0\x9F\x98\x8E for co...
-
35
2019年,德国 前段时间,遇到朋友的求助,说以前运行的好好的系统现在出问题了,而且看不懂报错到底是什么意思。 我仔细看看报错...
-
25
整理 MySQL 8.0 文档时发现一个变更: 默认字符集由 latin1 变为 utf8mb4。 想起以前整理过字符集转换文档,升级到 MySQL 8.0 后大概率会有字符集转换的需求,在此正好分享一下。 当时...
-
5
mysql字符集设置utf8 和 utf8mb4区别 目录 1.问题引起 mysql5.7版本,字符集设置为utf8,在执行emoji表情符(4字节的表情字符串)报错误: sql execute failure, exception:Incorrect strin...
-
10
mysql字符集utf8mb4失效踩坑 祈雨的博客 2019-08-17
-
3
Nic Lin's Blog喜歡在地上滾的工程師承上篇提到...
-
7
01月18, 2018 0 comments Mac下MySQL 5.7设置支持emoji表...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK