0

我正在尝试执行以下操作:

  1. 获取输入到 userField 中的用户名
  2. 使用用户名作为变量进行搜索 mysql_query

我很难通过第 2 阶段,因为 mysql_query 将 const char* 作为查询字符串,而我只能将用户名作为 char* 或 wchar_t*

我也在用unicode编译。

我现在的代码:

void mysql_connect(HWND hLoginWnd) {

MYSQL *con, mysql;
MYSQL_RES *res;
mysql_init(&mysql);

mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");

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

char name[512], pass[512];
int lenUser = SendMessage(userField, WM_GETTEXT, 512, (LPARAM)name);
int lenPass = SendMessage(passField, WM_GETTEXT, 512, (LPARAM)pass);

if(lenUser > 0 && lenPass > 0) {

    std::string query = "SELECT pass FROM users WHERE name='" + std::string(name) + "'";

    mysql_query(&mysql, query.c_str());

}

}

我应该怎么办?

4

1 回答 1

1

您将 SQL 字符集设置为 UTF-8,因此用于WideCharToMultiByte()将检索到的 UI Unicode 字符串转换为 UTF-8,然后将转换后的数据传递给数据库查询。UTF-8 编码的数据可以使用char缓冲区存储,您可以在预期 a 的地方传递 a char*(或char[]) 。const char*例如:

std::string Utf8Encode(WCHAR *wStr, int wLen)
{
    int utf8len = WideCharToMultiByte(CP_UTF8, 0, wStr, wLen, NULL, 0, NULL, NULL);
    if (utf8len > 0)
    {
        std::vector<char> utf8(utf8len);
        utf8len = WideCharToMultiByte(CP_UTF8, 0, wStr, wLen, &utf8[0], utf8len, NULL, NULL);
        if (utf8len > 0)
            return std::string(&utf8[0], utf8len);
    }
    return std::string();
}

void mysql_connect(HWND hLoginWnd)
{
    MYSQL *con, mysql;
    MYSQL_RES *res;
    mysql_init(&mysql);

    mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");

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

    WCHAR name[512];
    int lenUser = SendMessage(userField, WM_GETTEXTW, 512, (LPARAM)name);

    if (lenUser > 0)
    {
        std::string query = "SELECT pass FROM users WHERE name='" + Utf8Encode(name, lenUser) + "'";
        mysql_query(&mysql, query.c_str());
        ...
    }
}

话虽如此,在动态构建 SQL 语句时确实需要转义字符串。它更安全,因为它不容易受到 SQL 注入攻击:

std::string Utf8EncodeAndEscape(MYSQL *mysql, WCHAR *wStr, int wLen)
{
    int utf8len = WideCharToMultiByte(CP_UTF8, 0, wStr, wLen, NULL, 0, NULL, NULL);
    if (utf8len > 0)
    {
        std::vector<char> utf8(utf8len);
        utf8len = WideCharToMultiByte(CP_UTF8, 0, wStr, wLen, &utf8[0], utf8len, NULL, NULL);
        if (utf8len > 0)
        {
            std::vector<char> escaped(utf8len*2+1);
            unsigned long escapedLen = mysql_real_escape_string(mysql, &escaped[0], &utf8[0], utf8len);
            if (escapedLen > 0)
                return std::string(&escaped[0], escapedLen);
        }
    }

    return std::string();
}

void mysql_connect(HWND hLoginWnd)
{
    MYSQL *con, mysql;
    MYSQL_RES *res;
    mysql_init(&mysql);

    mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");

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

    WCHAR name[512];
    int lenUser = SendMessage(userField, WM_GETTEXTW, 512, (LPARAM)name);

    if (lenUser > 0)
    {
        std::string query = "SELECT pass FROM users WHERE name='" + Utf8EncodeAndEscape(&mysql, name, lenUser) + "'";
        mysql_query(&mysql, query.c_str());
        ...
    }
}

参数化查询同样安全,但比动态 SQL 语句更有效,尤其是当您需要多次执行同一语句时:

std::wstring Utf8Decode(char *utf8Str, int utf8Len)
{
    int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8Str, utf8Len, NULL, 0);
    if (wLen > 0)
    {
        std::vector<wchar_t> wStr(wLen);
        wLen = MultiByteToWideChar(CP_UTF8, 0, utf8Str, utf8Len, &wStr[0], wLen);
        if (wLen > 0)
            return std::wstring(&wStr[0], wLen);
    }
    return std::wstring();
}

MYSQL_STMT *stmt = mysql_stmt_init(&mysql);
if (stmt)
{    
    std::string query = "SELECT pass FROM users WHERE name=?"
    if (mysql_stmt_prepare(stmt, query.c_str(), query.length()) == 0)
    {
        if (mysql_stmt_param_count(stmt) == 1)
        {
            std::string utf8User = Utf8Encode(name, lenUser);
            unsigned long utf8len = utf8User.length();
            MYSQL_BIND param = {0};
            param.buffer_type = MYSQL_TYPE_STRING;
            param.buffer = utf8User.c_str();
            param.buffer_length = utf8len;
            param.length = &utf8len;
            param.is_unsigned = true;
            mysql_stmt_bind_param(stmt, &param);

            char result[512];
            unsigned long resultLen = 0;
            MYSQL_BIND result = {0};
            param.buffer_type = MYSQL_TYPE_STRING;
            param.buffer = &result[0];
            param.buffer_length = 512;
            param.length = &resultlen;
            param.is_unsigned = true;
            mysql_stmt_bind_result(stmt, &result);

            if (mysql_stmt_execute(stmt) == 0)
            {
                mysql_stmt_fetch(stmt);
                mysql_stmt_free_result(stmt); 

                SendMessage(passField, WM_SETTEXTW, 0, (LPARAM) Utf8Decode(result, resultLen).c_str());
            }
        }
    }
}

mysql_stmt_close(stmt); 
于 2013-11-05T01:17:51.157 回答