Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++:Win32应用程序中MySQL表检索的数据_C++_Mysql_Winapi_Unicode - Fatal编程技术网

C++:Win32应用程序中MySQL表检索的数据

C++:Win32应用程序中MySQL表检索的数据,c++,mysql,winapi,unicode,C++,Mysql,Winapi,Unicode,我正在尝试使用MySQL连接器/C从MySQL数据库检索数据。 一切正常,但我收到的所有数据都是胡言乱语: 以下是我的mysql连接和数据接收代码: void mysql_connect(HWND hwnd) { MYSQL mysql; mysql_init(&mysql); mysql_real_connect(&mysql, "localhost", "root", "", "thetable", 3306, NULL, 0); mysql_query(&my

我正在尝试使用MySQL连接器/C从MySQL数据库检索数据。 一切正常,但我收到的所有数据都是胡言乱语:

以下是我的mysql连接和数据接收代码:

void mysql_connect(HWND hwnd) {

MYSQL mysql;
mysql_init(&mysql);

mysql_real_connect(&mysql, "localhost", "root", "", "thetable", 3306, NULL, 0);

mysql_query(&mysql, "SELECT name, pass FROM users ORDER BY id ASC");

MYSQL_RES *res = mysql_store_result(&mysql);

if (res == NULL) 
{
    wchar_t err[64];
    swprintf(err, 64, L"%p", mysql_error(&mysql));
    MessageBox(NULL, err, L"Error", (UINT) MB_OK | MB_ICONINFORMATION);
    mysql_close(&mysql);
    return;
}

int num_fields = mysql_num_fields(res);

MYSQL_ROW row;

while ((row = mysql_fetch_row(res))) 
{ 
    for(int i = 0; i < num_fields; i++) 
    {
        wchar_t r[128];
        swprintf(r, 128, L"%p", row[i]);
        MessageBox(NULL, r, L"Data", (UINT) MB_OK | MB_ICONINFORMATION);
    } 
}

mysql_free_result(res);
mysql_close(&mysql);

}
在互联网上漫游了无数个mancycles之后,我了解到我应该将widechars中的数据转换成,因为我是用unicode编译的

我所有的MySQL表目前都用utf8\U unicode\U ci编码

我可以从程序接收的字符串示例:0108F164

那么,我应该如何以适当的方式获取数据呢


哦,如果我用ansi编译,收到的数据显示正确。

根据您的描述,听起来MySQL返回UTF-8字符串,您将其解释为UTF-16字符串。您需要运行它来将UTF8转换为UTF-16

MYSQL_ROW row;

while ((row = mysql_fetch_row(res))) 
{ 
    for(int i = 0; i < num_fields; i++) 
    {
        wchar_t r[128];
        if (MultiByteToWideChar(CP_UTF8, 0, row[i], -1, r, ARRAYSIZE(r)) != 0)
        {
            // success!
            MessageBox(NULL, r, L"Data", (UINT) MB_OK | MB_ICONINFORMATION);
        }
        else
        {
             DWORD err = GetLastError();
             swprintf(r, 128, L"Error %x", err);
             MessageBox(NULL, r, L"Error", (UINT) MB_OK | MB_ICONERROR);
        }
    } 
}

似乎MultiByteToWideChar始终返回0。使用这段代码并不能为我创建一个MessageBox。mysql表中有5个条目。@OlaviMustanoja如果MBTWC返回0,将设置GetLastError。它显示了什么?实际上我只是在做。错误为1004:无效的Flagsry 0而不是MB_预合成。