0
string StartTime = res->getDate("StartTime");
string LastModified = res->getDate("LastModified");
string Id = res->getInt("Id");
string PatientId = res->getInt("PatientId");

Following document claims these things work, but Visual Studio compiler using 8.0 mysql connector does not seem to have constructors for them. - https://docs.oracle.com/cd/B12037_01/appdev.101/b10778/reference025.htm

this seem to not provide any info why datetimes and ID's does not get returned... https://dev.mysql.com/doc/connector-cpp/1.1/en/connector-cpp-examples-results.html

Following fields get returned - 
string FILE_NAME = res->getString("FileName");
string VisitID = res->getString("VisitID");

the method I'm calling that should do the stuff

void calldatab() {
    for (auto& p : fs::recursive_directory_iterator("C:\\folder\\")) {
        if (p.path().extension() == ".pdf") {
            std::string element = p.path().string();
            size_t end_pos = element.rfind("end");
            string str2 = element.substr(42, end_pos);
            //std::cout << str2;
            string str3 = delSpaces(str2);

            //cout << str3;
            try
            {
                sql::Driver* driver;
                sql::Connection* con;
                //sql::Statement *stmt;
                sql::ResultSet* res;
                sql::PreparedStatement* pstmt;

                /* Create a connection */
                driver = get_driver_instance();
                con = driver->connect("tcp://127.0.0.1:3306", "root", "");
                /* Connect to the MySQL test database */
                con->setSchema("semaserver");
                pstmt = con->prepareStatement("");
                //pstmt->setInt(1, 1);
                pstmt->setString(1, str3);
                res = pstmt->executeQuery();

                /* Fetch in reverse = descending order! */

                ///cikls kur izmantos mysql datu masvu
                //res->afterLast();

                    while (res->next()) {
                        string FILE_NAME = res->getString("FileName");
                        //cout << FILE_NAME;
                        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");
                        std::string cmd = "copy /-y " + element + " " + "C:\\PACIENTI\\" + PatientId + '-' + StartTime + '-' + VisitID + '-' + LastModified + ".pdf";

                        for (auto& p2 : fs::directory_iterator("C:\\folder\\")) {
                            if (element != p2.path().string()) {
                                cout << cmd;
                                FILE* pipe = _popen(cmd.c_str(), "r");

                                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& 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.message();
                cout << "# ERR: SQLException in creation" << endl;
                //cout << e.message;
            }
        }
    }
}

with DATE and TIME fields in actual database I get failed connection to localhost, so how do I get this info? MySQL shell returns this info.

Ronalds Mazītis
  • 323
  • 2
  • 5
  • 18

1 Answers1

0

Oracle C++ Call Interface and MySQL Connector are two different things. The website you linked is for Oracle, not MySQL. They could have completely different interfaces. You really want to make sure you are looking at the right documentation.


To get datetime, MySQL Connector does not have a getDate or similar function. Instead, you have to use getString and manually parse it. And it should be in the format: %Y-%m-%d %H:%M:%S.

The Connector does have getInt. However, it returns a int32_t, so you would need to change the type of Id and PatientId to integer types. If you insist to have Id as string type, then you need to do:

string Id = std::to_string(res->getInt("Id"));

Also you can consider using the X devapi, which is included with the connector, to better utilize some modern c++ features.

A complete example can be found here: https://dev.mysql.com/doc/dev/connector-cpp/8.0/devapi_ref.html

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
  • Okay, dude but I still can't get date time and int fields from MySQL database using the connector and it's documentation. Comprehend and please give me more hints on this nice thing, cause it certainly does not answer my questions, been through it. – Ronalds Mazītis May 20 '21 at 20:47
  • 1
    @RonaldsMazītis updated the answer, hope it will help – Ranoiaetep May 20 '21 at 21:26