開發平台(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;
}