SEEMS LIKE Latin1 ISO-8859-1 can't even save special characters so format of the database must be Latin7 ISO-8859-7. Could not really find easy function to do this, do I really have to write one myself?
UPDATE, UPDATE --- I made small progress as described in this question article - Special characters in Visual Studio 2019 C++ project AND executing CMD commands with them
BUT THE PROBLEM SEEMS TO APPEAR ON DEFAULT PROJECT SETTINGS without any mysql library's or anything, IN ALL CORRECT CODED FILES. (UTF8) EVEN WHEN COMPILE FLAGS ARE ADDED, EVEN WHEN "FIX FILE ENCODING" IS INSTALLED.
#include <iostream>
int main() {
string output = "āāāčččēēēē";
cout << output << endl;
}
Intro rant* - This is 3rd post about MySql Connector, because I just could not find basic information about MySql Connector in Google at all (MySQL and MariaDB library's in C++ using cmake, mingw), first there was no explanation that GCC will not be able to compile it for Windows systems, then I had no luck finding how to use datetime and int objects in the output from database, until I posted issue here (How to return time, date data fields in c++ mysql oracle vs17?).
My issue now is that strings returned from database have special characters - āàčīēļš etc. Column:test2col Collation:Latin7_general_ci
So here is the code that might work, but does not due to table or something wrong, any expertise might help:
#include <iostream>
#include <string>
#include <string_view>
std::string_view itou[256] {
{"\x00",1} , {"\x01",1} , {"\x02",1} , {"\x03",1} ,
{"\x04",1} , {"\x05",1} , {"\x06",1} , {"\x07",1} ,
{"\x08",1} , {"\x09",1} , {"\x0a",1} , {"\x0b",1} ,
{"\x0c",1} , {"\x0d",1} , {"\x0e",1} , {"\x0f",1} ,
{"\x10",1} , {"\x11",1} , {"\x12",1} , {"\x13",1} ,
{"\x14",1} , {"\x15",1} , {"\x16",1} , {"\x17",1} ,
{"\x18",1} , {"\x19",1} , {"\x1a",1} , {"\x1b",1} ,
{"\x1c",1} , {"\x1d",1} , {"\x1e",1} , {"\x1f",1} ,
{"\x20",1} , {"\x21",1} , {"\x22",1} , {"\x23",1} ,
{"\x24",1} , {"\x25",1} , {"\x26",1} , {"\x27",1} ,
{"\x28",1} , {"\x29",1} , {"\x2a",1} , {"\x2b",1} ,
{"\x2c",1} , {"\x2d",1} , {"\x2e",1} , {"\x2f",1} ,
{"\x30",1} , {"\x31",1} , {"\x32",1} , {"\x33",1} ,
{"\x34",1} , {"\x35",1} , {"\x36",1} , {"\x37",1} ,
{"\x38",1} , {"\x39",1} , {"\x3a",1} , {"\x3b",1} ,
{"\x3c",1} , {"\x3d",1} , {"\x3e",1} , {"\x3f",1} ,
{"\x40",1} , {"\x41",1} , {"\x42",1} , {"\x43",1} ,
{"\x44",1} , {"\x45",1} , {"\x46",1} , {"\x47",1} ,
{"\x48",1} , {"\x49",1} , {"\x4a",1} , {"\x4b",1} ,
{"\x4c",1} , {"\x4d",1} , {"\x4e",1} , {"\x4f",1} ,
{"\x50",1} , {"\x51",1} , {"\x52",1} , {"\x53",1} ,
{"\x54",1} , {"\x55",1} , {"\x56",1} , {"\x57",1} ,
{"\x58",1} , {"\x59",1} , {"\x5a",1} , {"\x5b",1} ,
{"\x5c",1} , {"\x5d",1} , {"\x5e",1} , {"\x5f",1} ,
{"\x60",1} , {"\x61",1} , {"\x62",1} , {"\x63",1} ,
{"\x64",1} , {"\x65",1} , {"\x66",1} , {"\x67",1} ,
{"\x68",1} , {"\x69",1} , {"\x6a",1} , {"\x6b",1} ,
{"\x6c",1} , {"\x6d",1} , {"\x6e",1} , {"\x6f",1} ,
{"\x70",1} , {"\x71",1} , {"\x72",1} , {"\x73",1} ,
{"\x74",1} , {"\x75",1} , {"\x76",1} , {"\x77",1} ,
{"\x78",1} , {"\x79",1} , {"\x7a",1} , {"\x7b",1} ,
{"\x7c",1} , {"\x7d",1} , {"\x7e",1} , {"\x7f",1} ,
{"\xc2""\x80",2} , {"\xc2""\x81",2} , {"\xc2""\x82",2} , {"\xc2""\x83",2} ,
{"\xc2""\x84",2} , {"\xc2""\x85",2} , {"\xc2""\x86",2} , {"\xc2""\x87",2} ,
{"\xc2""\x88",2} , {"\xc2""\x89",2} , {"\xc2""\x8a",2} , {"\xc2""\x8b",2} ,
{"\xc2""\x8c",2} , {"\xc2""\x8d",2} , {"\xc2""\x8e",2} , {"\xc2""\x8f",2} ,
{"\xc2""\x90",2} , {"\xc2""\x91",2} , {"\xc2""\x92",2} , {"\xc2""\x93",2} ,
{"\xc2""\x94",2} , {"\xc2""\x95",2} , {"\xc2""\x96",2} , {"\xc2""\x97",2} ,
{"\xc2""\x98",2} , {"\xc2""\x99",2} , {"\xc2""\x9a",2} , {"\xc2""\x9b",2} ,
{"\xc2""\x9c",2} , {"\xc2""\x9d",2} , {"\xc2""\x9e",2} , {"\xc2""\x9f",2} ,
{"\xc2""\xa0",2} , {"\xe2""\x80""\x98",3}, {"\xe2""\x80""\x99",3}, {"\xc2""\xa3",2} ,
{"\xe2""\x82""\xac",3}, {"\xe2""\x82""\xaf",3}, {"\xc2""\xa6",2} , {"\xc2""\xa7",2} ,
{"\xc2""\xa8",2} , {"\xc2""\xa9",2} , {"\xcd""\xba",2} , {"\xc2""\xab",2} ,
{"\xc2""\xac",2} , {"\xc2""\xad",2} , {"\x3f",1} , {"\xe2""\x80""\x95",3},
{"\xc2""\xb0",2} , {"\xc2""\xb1",2} , {"\xc2""\xb2",2} , {"\xc2""\xb3",2} ,
{"\xce""\x84",2} , {"\xce""\x85",2} , {"\xce""\x86",2} , {"\xc2""\xb7",2} ,
{"\xce""\x88",2} , {"\xce""\x89",2} , {"\xce""\x8a",2} , {"\xc2""\xbb",2} ,
{"\xce""\x8c",2} , {"\xc2""\xbd",2} , {"\xce""\x8e",2} , {"\xce""\x8f",2} ,
{"\xce""\x90",2} , {"\xce""\x91",2} , {"\xce""\x92",2} , {"\xce""\x93",2} ,
{"\xce""\x94",2} , {"\xce""\x95",2} , {"\xce""\x96",2} , {"\xce""\x97",2} ,
{"\xce""\x98",2} , {"\xce""\x99",2} , {"\xce""\x9a",2} , {"\xce""\x9b",2} ,
{"\xce""\x9c",2} , {"\xce""\x9d",2} , {"\xce""\x9e",2} , {"\xce""\x9f",2} ,
{"\xce""\xa0",2} , {"\xce""\xa1",2} , {"\x3f",1} , {"\xce""\xa3",2} ,
{"\xce""\xa4",2} , {"\xce""\xa5",2} , {"\xce""\xa6",2} , {"\xce""\xa7",2} ,
{"\xce""\xa8",2} , {"\xce""\xa9",2} , {"\xce""\xaa",2} , {"\xce""\xab",2} ,
{"\xce""\xac",2} , {"\xce""\xad",2} , {"\xce""\xae",2} , {"\xce""\xaf",2} ,
{"\xce""\xb0",2} , {"\xce""\xb1",2} , {"\xce""\xb2",2} , {"\xce""\xb3",2} ,
{"\xce""\xb4",2} , {"\xce""\xb5",2} , {"\xce""\xb6",2} , {"\xce""\xb7",2} ,
{"\xce""\xb8",2} , {"\xce""\xb9",2} , {"\xce""\xba",2} , {"\xce""\xbb",2} ,
{"\xce""\xbc",2} , {"\xce""\xbd",2} , {"\xce""\xbe",2} , {"\xce""\xbf",2} ,
{"\xcf""\x80",2} , {"\xcf""\x81",2} , {"\xcf""\x82",2} , {"\xcf""\x83",2} ,
{"\xcf""\x84",2} , {"\xcf""\x85",2} , {"\xcf""\x86",2} , {"\xcf""\x87",2} ,
{"\xcf""\x88",2} , {"\xcf""\x89",2} , {"\xcf""\x8a",2} , {"\xcf""\x8b",2} ,
{"\xcf""\x8c",2} , {"\xcf""\x8d",2} , {"\xcf""\x8e",2} , {"\x3f",1}
};
int main() {
std::string input{"āāāčččēēēē"};
std::string output;
for (auto c : input) {
output.append(itou[static_cast<uint8_t>(c)]);
}
std::cout << output << std::endl;
}
string FirstName = res->getString("test2col");
Documentation for MySQL Connector: https://dev.mysql.com/doc/dev/connector-cpp/8.0/
Seem to not tell much about this, so thanks for any help!
So here is code example that turns into another error based on solutions in commentaries -
157
#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <cstring>
#include <filesystem>
#include <codecvt>
#include <cstdint>
#include <locale>
Severity Code Description Project File Line Suppression State Error C4996 'std::wstring_convert<std::codecvt_utf8<wchar_t,1114111,(std::codecvt_mode)0>,wchar_t,std::allocator<wchar_t>,std::allocator<char>>::to_bytes': warning STL4017: std::wbuffer_convert, std::wstring_convert, and the <codecvt> header (containing std::codecvt_mode, std::codecvt_utf8, std::codecvt_utf16, and std::codecvt_utf8_utf16) are deprecated in C++17. (The std::codecvt class template is NOT deprecated.) The C++ Standard doesn't provide equivalent non-deprecated functionality; consider using MultiByteToWideChar() and WideCharToMultiByte() from <Windows.h> instead. You can define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.
```cpp
try
{
std::unique_ptr<sql::Connection> connection{ nullptr };
try {
sql::Driver* driver = ::get_driver_instance();
//sql::Connection* con;
//sql::Statement *stmt;
//sql::ResultSet* res;
//sql::Statement* pstmt;
sql::ConnectOptionsMap connection_options{};
connection_options["hostName"] = "tcp://127.0.0.1:3306"; // Replace with your log-in
connection_options["userName"] = "root"; // ...
connection_options["password"] = "parole123!"; // ...
connection_options["schema"] = "test"; // ...
connection_options["characterSetResults"] = "latin7_general_ci";
connection_options["OPT_CHARSET_NAME"] = "latin7_general_ci";
connection_options["OPT_SET_CHARSET_NAME"] = "latin7_general_ci";
connection.reset(driver->connect(connection_options));
driver = get_driver_instance();
/* Create a connection */
//con = driver->connect("tcp://127.0.0.1:3306", "root", "parole123!");
//con->setClientOption("characterSetResults", "UTF8");
/* Connect to the MySQL test database */
//con->setSchema("test");
//pstmt = con->createStatement();
std::string const some_query = "SELECT * FROM test2";
std::unique_ptr<sql::Statement> statement{ connection->createStatement() };
//res = pstmt->executeQuery("SELECT * FROM test2");
std::unique_ptr<sql::ResultSet> res{ statement->executeQuery(some_query) };
//pstmt->setInt(1, 1);
//pstmt->setString(1, str2);
//res = pstmt->executeQuery();
/* Fetch in reverse = descending order! */
///cikls kur izmantos mysql datu masvu
//res->afterLast();
while (res->next()) {
std::string const FILE_NAME = res->getString("test2col");
string locations2 = ("C:\\Users\\Janis\\Desktop\\TEST2\\");
string txtt = (".txt");
string copy2 = ("copy /-y ");
string space = " ";
string PACIENTI2 = "C:\\PACIENTI\\";
string element = copy2 + locations2 + FILE_NAME + txtt;
//string StartTime = res->getString("StartTime");
//string VisitID = res->getString("VisitID");
//string LastModified = res->getString("LastModified");
//string Id = res->getString("Id");
//string PatientId = res->getString("PatientId");
for (auto& p2 : fs::directory_iterator("C:\\Users\\Janis\\Desktop\\TEST2\\")) {
if (FILE_NAME != p2.path().string()) {
string cmd = element + space + PACIENTI2 + FILE_NAME + txtt;
FILE* pipe = _popen(cmd.c_str(), "r");
cout << cmd << endl;
/*if (pipe == NULL)
{
return;
}
char buffer[128];
std::string result = "";
while (!feof(pipe))
{
if (fgets(buffer, 128, pipe) != NULL)
{
result += buffer;
}
}*/
//std::cout << "Results: " << std::endl << result << std::endl ;
//_pclose(pipe);
}
}
}
//delete res;
//delete pstmt;
//delete con;
}
catch (sql::SQLException& ex) {
std::cerr << "Error occured when connecting to SQL data base: " << ex.what() << "(" << ex.getErrorCode() << ").";
}
}
catch (sql::SQLException& e)
{
///nav implementēts vairāk info
//cout << "# ERR: SQLException in " << __FILE__;
//cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
/* what() (derived from std::runtime_error) fetches error message */
//cout << "# ERR: " << e.what();
//cout << " (MySQL error code: " << e.getErrorCode();
cout << "# ERR: SQLException in " << endl;
}
copy /-y C:\Users\username\Desktop\TEST2\J─ünis.txt C:\PACIENTI\J─ünis.txt
copy /-y C:\Users\username\Desktop\TEST2\Ann─ü.txt C:\PACIENTI\Ann─ü.txt
instead it should be
copy /-y C:\Users\Janis\Desktop\TEST2\Jānis.txt C:\PACIENTI\Jānis.txt
copy /-y C:\Users\Janis\Desktop\TEST2\Annā.txt C:\PACIENTI\Annā.txt