開發平台(Platform): (Ex: Win10, Linux, ...)
win7 64bit
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
qt 5.8.0 mingw 32bit
xmapp 7.1.1
Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1
MariaDB 伺服器字元集: UTF-8 Unicode (utf8)
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
在phpMyadmin裡面中文顯示正常,欄位和資料庫的編碼都有設定為utf8_unicode_ci
qt5 之後的預設應該都是utf-8編碼了,照理說應該不會有中文亂碼問題
但是query出來後的中文資料全部都亂碼
餵入的資料(Input):
預期的正確結果(Expected Output):
Opened
工址
構造
Closing...
錯誤結果(Wrong Output):
Opened
"撌亙"
"瑽\uF561\u0080"
Closing...
程式碼(Code):(請善用置底文網頁, 記得排版)
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setUserName("admin");
db.setPassword("0000");
db.setDatabaseName("test");
if(db.open())
{
qDebug()<<"Opened";
QSqlQuery qry;
if(qry.exec("SELECT `Col_Name` FROM `column_table` WHERE
`Category_ID`=01 AND `Authority`=0"))
{
while(qry.next())
{
qDebug()<< qry.value(0).toString().trimmed();
}
}
else
{
qDebug()<<"error: "<<qry.lastError().text();
}
qDebug()<<"Closing...";
db.close();
}
else
{
qDebug()<<"error: "<<db.lastError().text();
}
補充說明(Supplement):
有試過在前面加上
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
但是出來的內容一樣是亂碼
Opened
"撌亙?"
"瑽鹉耿
Closing...
有測試過用sqlite3 資料庫內容一樣為中文
用qdebug輸出顯示是正常的中文
不知道到底哪個環節設定錯誤…
卡關好久 orz
有沒有人知道怎麼解?
補充
直接用qDebug()<< qry.value(0).toByteArray();
顯示輸出
Opened
"\xE6\x92\x8C\xE4\xBA\x99"
"\xE7\x91\xBD\xEF\x95\xA1\xC2\x80"
Closing...
若加上
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
顯示輸出變成
Opened
"\xE5\xB7\xA5\xE5\x9D\x80"
"\xE6\xA7\x8B\xE9\x80\xA0"
Closing...
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.233.182.226
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1488387465.A.1A4.html
→
03/02 01:38, , 1F
03/02 01:38, 1F
→
03/02 01:39, , 2F
03/02 01:39, 2F
→
03/02 01:43, , 3F
03/02 01:43, 3F
※ 編輯: ppi3106 (36.233.182.226), 03/02/2017 01:53:11
→
03/02 01:53, , 4F
03/02 01:53, 4F
→
03/02 02:24, , 5F
03/02 02:24, 5F
→
03/02 02:57, , 6F
03/02 02:57, 6F
→
03/02 02:57, , 7F
03/02 02:57, 7F
但是因為用qry.value抓下來的時候,他function就預設是QString了
設了codec之後,直接印qry.value(0)顯示的會是如下
Opened
QVariant(QString, "撌亙?")
QVariant(QString, "瑽鹉耿)
Closing...
所以其實懷疑是不是mysql的設置上哪裡出錯,但是編碼又都設為utf8了…
→
03/02 02:59, , 8F
03/02 02:59, 8F
→
03/02 03:00, , 9F
03/02 03:00, 9F
推
03/02 08:02, , 10F
03/02 08:02, 10F
→
03/02 11:56, , 11F
03/02 11:56, 11F
→
03/02 11:57, , 12F
03/02 11:57, 12F
→
03/02 11:58, , 13F
03/02 11:58, 13F
※ 編輯: ppi3106 (36.233.182.226), 03/02/2017 12:03:39
推
03/23 20:23, , 14F
03/23 20:23, 14F
→
03/23 20:47, , 15F
03/23 20:47, 15F
→
03/23 20:47, , 16F
03/23 20:47, 16F
→
03/23 20:55, , 17F
03/23 20:55, 17F
→
03/23 20:55, , 18F
03/23 20:55, 18F
→
03/23 21:31, , 19F
03/23 21:31, 19F
→
03/23 21:31, , 20F
03/23 21:31, 20F