1、QSqlDatabase对象设置UTF-8编码
QSqlDatabase db;
db.exec("SET NAMES 'UTF-8'");
2、将MySQL编码设置成UTF-8
show variables like 'character_set_%';
set character_set_client=utf-8;
set character_set_connection=utf-8;
set character_set_database=utf-8;
set character_set_results=utf-8;
set character_set_server=utf-8;
set collation_database = utf8mb4_unicode_ci;
set collation_connection = utf8mb4_unicode_ci;
3、MySQL的my.ini文件或my.cnf设置UTF-8
# 设置mysql客户端默认字符集
default-character-set=utf8
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
4、头文件设置UTF-8
#pragma execution_character_set("utf-8")
5、使用QString::fromLocal8Bit()
QString sql = QString::fromLocal8Bit("SELECt * FROM table");