如何在c++ mysql oracle vs17中返回时间、日期数据字段? [英] How to return time, date data fields in c++ mysql oracle vs17?

查看:24
本文介绍了如何在c++ mysql oracle vs17中返回时间、日期数据字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下文档声称这些东西有效,但使用 8.0 mysql 连接器的 Visual Studio 编译器似乎没有它们的构造函数.- https://docs.oracle.com/cd/B12037_01/appdev.101/b10778/reference025.htm

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

这似乎没有提供任何信息为什么日期时间和 ID 没有被返回......https://dev.mysql.com/doc/connector-cpp/1.1/en/connector-cpp-examples-results.html

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;
            }
        }
    }
}

在实际数据库中使用 DATE 和 TIME 字段时,我无法连接到本地主机,那么如何获取此信息?MySQL shell 返回此信息.

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.

推荐答案

Oracle C++ Call InterfaceMySQL Connector 是两个不同的东西.您链接的网站适用于 Oracle,而不是 MySQL.它们可以有完全不同的接口.您确实想确保您正在查看正确的文档.

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.

要获取日期时间,MySQL 连接器没有 getDate 或类似功能.相反,您必须使用 getString 并手动解析它.它的格式应该是:%Y-%m-%d %H:%M:%S.

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.

连接器确实有 getInt.但是,它返回一个 int32_t,因此您需要将 IdPatientId 的类型更改为整数类型.如果你坚持将 Id 作为 string 类型,那么你需要这样做:

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"));


您也可以考虑使用包含在连接器中的 X devapi,以更好地利用一些现代 C++ 功能.


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

一个完整的例子可以在这里找到:https:///dev.mysql.com/doc/dev/connector-cpp/8.0/devapi_ref.html

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

这篇关于如何在c++ mysql oracle vs17中返回时间、日期数据字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆