あなたの場合の問題はstd::wstringとは関係がないと思います :8ビットのstd::string UTF-8には十分なはずです(単純なstd::stringを作成します 特殊文字"āàčīēļš" オペレーティングシステムによって異なりますが、std::wstringは正常に動作します) は2バイト(Windows)または4バイト(Linux)です(詳細はこちら
およびgetStringを見ると 関数は、sql::SQLStringを受け取って返すことがわかります。 。 sql::SQLString クラスは、std::stringの単純なラッパーです。 。
utf-8を指定する必要があると思います MySqlのデフォルトの文字セットとして :このためには、次の接続オプション
データベースに接続する場合:
std::unique_ptr<sql::Connection> connection {nullptr};
try {
sql::Driver* driver = ::get_driver_instance();
sql::ConnectOptionsMap connection_options {};
connection_options["hostName"] = url; // Replace with your log-in
connection_options["userName"] = username; // ...
connection_options["password"] = password; // ...
connection_options["schema"] = schema; // ...
connection_options["characterSetResults"] = "utf8";
connection_options["OPT_CHARSET_NAME"] = "utf8";
connection_options["OPT_SET_CHARSET_NAME"] = "utf8";
connection.reset(driver->connect(connection_options));
} catch (sql::SQLException& ex) {
std::cerr << "Error occured when connecting to SQL data base: " << ex.what() << "(" << ex.getErrorCode() << ").";
}
その後、次のようにデータベースのクエリを続行できるようになります
std::string const some_query = "SELECT * FROM some_table_name;";
std::unique_ptr<sql::Statement> statement {connection->createStatement()};
std::unique_ptr<sql::ResultSet> result {statement->executeQuery(some_query)};
while (result->next()) {
std::string const some_field = result->getString("some_field_name");
// Process: e.g. display with std::cout << some_field << std::endl;
}
それを使用してファイル名を作成したり、コンソールに出力したりするときに発生する問題は、 Windowsです。 それ自体(以前はLinuxのみでコードをテストしたことがあるため、以前はこの問題に遭遇しませんでした!):デフォルトでは、UTF-8ではなくANSIを使用します。 āàčīēļšのようなものを出力しても std::coutを使用している場合でも、正しく出力されません。 またはstd::wcout std::wstringと組み合わせて 。代わりに、─ü├á─ì─½─ô─╝┼íを出力します 。
バイトを抽出する場合
void dump_bytes(std::string const& str) {
std::cout << std::hex << std::uppercase << std::setfill('0');
for (unsigned char c : str) {
std::cout << std::setw(2) << static_cast<int>(c) << ' ';
}
std::cout << std::dec << std::endl;
return;
}
C4 81 C3 A0 C4 8D C4 AB C4 93 C4 BC C5 A1を出力します これ
などのbyte-to-utf8コンバーターに接続し直します。 実際にāàčīēļšを提供します 。したがって、文字列は正しく読み取られましたが、Windowsが正しく表示していません。次のセクションと最後のセクションの組み合わせ(utf-8を指定) MySqlのデフォルトの文字セットとして)すべての問題を修正する必要があります:
-
SetConsoleOutputCP(CP_UTF8);の呼び出しwindows.hから プログラムの開始時に、コンソール出力が修正されます :#include <cstdlib> #include <iostream> #include <string> #include <windows.h> int main() { // Forces console output to UTF8 SetConsoleOutputCP(CP_UTF8); std::string const name = u8"āàčīēļš"; std::cout << name << std::endl; // Actually outputs āàčīēļš return EXIT_SUCCESS; } -
同様に、ファイルを作成するルーチンを適応させる必要があります デフォルトではUTF8でもありません(ファイルの内容は問題になりませんが、ファイル名自体は問題になります!)。
std::ofstreamを使用しますfstreamからstd::filesystem::u8pathと組み合わせて C++17ライブラリのfilesystemから これを解決するには:#include <cstdlib> #include <filesystem> #include <fstream> #include <string> int main() { std::string const name = u8"āàčīēļš"; std::ofstream f(std::filesystem::u8path(name + ".txt")); // Creates a file āàčīēļš.txt f << name << std::endl; // Writes āàčīēļš to it return EXIT_SUCCESS; }