开发平台(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;
}