开发者

Get and set wchar_t chars from c++ to MySQL to c++

I have a database (Mysql) with some fields in UTF-8 (I can change that if needed).

I use an old MySQL 'Communicator' or its predecessor, I wrote the code ~3 years ago and all I know today is that I link with libmysql.lib and #include "mysql.h" (do tell if you want me to dig up more).

The thing is I don't have a clue about how to insert and read non-ASCII.

I know about MultiByteToWideChar but some code compiles under Linux (gcc) so that won't work I guess.

What is the standard procedure when it comes to getting and setting std::wstrings (or wchar_t*) from/to a MyS开发者_StackOverflow中文版QL database when the code should run on Linux and Windows ?


Use the mysql_set_character_set() API to change the connection character set to "utf8". Convert your wstrings to UTF-8 (there's no library function, but you can write it fairly easily, or use iconv), and pass UTF-8 pointers to MySQL.

This API isn't present in older versions of MySQL C++ API (>5.0.7), so check your header first.

Note: on Win32 with Visual Studio, the wchar_t datatype is short, not int. You can change the connection charset to "ucs2" and pass wchar_t pointers directly, by just casting them to char*.


I recommend that you store your data in MySQL in UTF-8 form, rather than try to convince it to take Windows-native UTF-16. If your text is mostly Western, UTF-8 encoding will be more compact, and it will be more likely to work with the C-and-Unix nature of MySQL.

There are Visual C++ specific examples in MySQL++ that show how to do this. See CExampleDlg::ToUTF8() and the functions that call it in examples/vstudio/mfc/mfc_dlg.cpp, for example.

There's a chapter in the MySQL++ user manual that gives the whys and wherefores behind all this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜