c++ - 如何将 Unicode 字符串连接成字符串以传递给 mysql 调用

标签 c++ visual-studio mariadb wxwidgets

我正在尝试将一个字符串数组连接成一个字符数组 - 但其中一个字符串是外语(为什么我需要 UTF8)。从数据库中读取 UTF8 字符串并将其放入 wxString 数组后,我可以在调试器 (Visual Studio) 中以适当的语言看到 UTF8 字符串,但是当我尝试将字符串连接到数组时,t 永远不会放在那里.

我试过 variable.mb_str() 变量.mb.str().data()。两者似乎都不适用于 strcat 对于我的语言数据。其他数据连接良好。所有数据均来自 MariaDB 数据库调用。

 int i, numRows;
 wxString query;
 wxString sortby;
 wxString group_list;
 wxString *stringGroups;
 char holdString[400];

 /*   Try UTF Force */
 query.Printf(_("set names 'utf8'"));
 mysql_query(mDb, query.mb_str());
 result = mysql_store_result(mDb);
 mysql_free_result(result);
 query.Printf(_("select GROUP_NAME from USER_PERMS where USER_NAME = 
    \"%s\" 
 ORDER BY GROUP_NAME "),  riv_getuser().c_str() );
 mysql_query(mDb, query.mb_str());   
 result = mysql_store_result(mDb);
 numRows = mysql_num_rows(result);
 stringGroups = new wxString[numRows + 1];
 i = 0; 
 while ((row = mysql_fetch_row(result)))      
 {        
    stringGroups[i] = wxString(row[0], wxConvUTF8);
    i++;
 }
 mysql_free_result(result);
 i = 0;
 strcpy (holdString,"IN (\'");
 while (i < numRows)
 {
   if (i != 0) strcat(holdString, "\', \'");
   strcat(holdString, (const char *)stringGroups[i].mb_str().data());
   i++;  
 }   
 strcat (holdString," \')");

-- END OF CODE --

--ACTUAL stringGroup that fails -- Debugger Watch Output
stringGroups[2] {m_impl=L"文字化け"... 

我希望得到:

IN ( 'test' , 'test' , '文字化け' )

我得到了什么

IN ( 'test','test2','' )

最佳答案

不要将strcpy()strcat()wxString 一起使用,这只会造成不必要的错误。如果您首先使用 wxString,构建您需要的整个字符串,然后使用 utf8_str() 方法获取包含 UTF-8 字符串内容的缓冲区,然后您可以将其传递到您需要的任何功能。

请记住,此缓冲区是临时的,因此如果您不复制它或至少延长其生命周期,则不能指望它继续存在,即

auto const& buf = some_wx_string.utf8_str();
... now you can use buf.data() safely until the end of scope ...

关于c++ - 如何将 Unicode 字符串连接成字符串以传递给 mysql 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55579601/

相关文章:

mysql - 在 SQL 中连接来自同一个表的两个外键

mysql - innodb 操作非常慢

c++ - 解码位图时宽度不正确

c++ - 对 WSACleanup() 的错误调用杀死了 WSAStartup()

c# - 如何启动 Windows 窗体应用程序?

c++ - Visual Studio 调试持续未处理的异常

MS Access 中联合查询的 MySQL 语法错误 1064

c++ - 将从 "midnight 1904-1-1"开始的秒数转换为日期时间字符串

java - 低功耗蓝牙 Java Api 或 C/C++ 库

visual-studio - 我怎样才能阻止 resharper 提示 html5 样板 html 标签?