问题描述
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
这似乎没有提供任何信息为什么日期时间和 ID 没有被返回...... 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");
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 返回此信息。
解决方法
Oracle C++ 调用接口 和 MySQL 连接器 是两个不同的东西。您链接的网站适用于 Oracle,而不是 MySQL。它们可以有完全不同的接口。您确实想确保您正在查看正确的文档。
- MySQL 连接器的文档:https://dev.mysql.com/doc/dev/connector-cpp/8.0/
要获取日期时间,MySQL 连接器没有 getDate
或类似功能。相反,您必须使用 getString
并手动解析它。它的格式应该是:%Y-%m-%d %H:%M:%S
.
连接器确实有 getInt
。但是,它返回一个 int32_t
,因此您需要将 Id
和 PatientId
的类型更改为整数类型。如果您坚持将 Id
作为 string
类型,那么您需要这样做:
string Id = std::to_string(res->getInt("Id"));
您也可以考虑使用包含在连接器中的 X devapi,以更好地利用一些现代 C++ 功能。
可以在此处找到完整示例:https://dev.mysql.com/doc/dev/connector-cpp/8.0/devapi_ref.html