開發平台(Platform): (Ex: Win10, Linux, ...)
Win10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
Visual Studio2013
問題(Question):
小弟想將日文字元存入資料庫
查了一下資料,知道需要轉碼,編碼成UTF8
在網路上找到一個範例程式
但是執行程式後存進去依舊出現亂碼
想請問各位大大問題錯在哪邊,該如何修改!
預期的正確結果(Expected Output):
日文字元存進資料庫不會變成亂碼
錯誤結果(Wrong Output):
http://imgur.com/a/C55Q6
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <windows.h>
#include <stdio.h>
#include <mysql.h>
int main() {
char user[] = "root";
char pswd[] = "sihuang0929";
char host[] = "192.168.0.37";
char table[] = "mms_yr";
char sos[MAX_PATH];
unsigned int port = 3306;
MYSQL myCont;
mysql_init(&myCont);
char *szData = "もしもし", *sendbuf_utf8 = NULL;
wchar_t *sendbuf_Unicode = NULL;
int nDataLen = MultiByteToWideChar(950, 0, szData, -1, NULL, 0);
sendbuf_Unicode = new wchar_t[nDataLen + 1];
MultiByteToWideChar(950, 0, szData, -1, sendbuf_Unicode, nDataLen);
nDataLen = WideCharToMultiByte(CP_UTF8, 0, sendbuf_Unicode, -1, NULL, 0,
NULL, NULL);
sendbuf_utf8 = new char[nDataLen + 1];
WideCharToMultiByte(CP_UTF8, 0, sendbuf_Unicode, -1, sendbuf_utf8, nDataLen,
NULL, NULL);
printf("%s", sendbuf_utf8);
if (mysql_real_connect(&myCont, host, user, pswd, table, port, NULL, 0))
{
mysql_query(&myCont, "SET NAMES BIG5");
sprintf_s(sos, "INSERT INTO testt SELECT NULL,'%s' FROM dual WHERE not
exists(select * from testt where First_Name = '%s');", sendbuf_utf8,
sendbuf_utf8);
mysql_query(&myCont, sos);
}
delete[]sendbuf_utf8; sendbuf_utf8 = NULL;
delete[]sendbuf_Unicode; sendbuf_Unicode = NULL;
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.116.59.114
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1495537848.A.D1A.html
→
05/23 19:35, , 1F
05/23 19:35, 1F
→
05/23 19:37, , 2F
05/23 19:37, 2F
→
05/23 19:39, , 3F
05/23 19:39, 3F
→
05/23 19:40, , 4F
05/23 19:40, 4F
→
05/23 19:40, , 5F
05/23 19:40, 5F
→
05/23 19:40, , 6F
05/23 19:40, 6F
好的,謝謝您的指導,我試著做看看
推
05/24 08:58, , 7F
05/24 08:58, 7F
→
05/24 08:59, , 8F
05/24 08:59, 8F
了解,謝謝您的指導。
小弟最後的目的是要用.cFileName抓到日文的檔案名稱入庫
前面說的不清楚,不好意思!
※ 編輯: rul3nk41 (1.163.38.217), 05/24/2017 11:40:57
推
05/25 06:57, , 9F
05/25 06:57, 9F
→
05/25 06:58, , 10F
05/25 06:58, 10F
→
05/25 06:59, , 11F
05/25 06:59, 11F
→
05/25 06:59, , 12F
05/25 06:59, 12F
了解了,謝謝您! ^^
※ 編輯: rul3nk41 (1.163.38.217), 05/26/2017 17:01:08