我正在尝试使用 8.0.13 MySQL C++ 连接器从数据库中读取数据。我能够成功写入数据库没有问题,但是当我尝试获取数据库的结果(使用下一个结果)时,它永远不会运行。
bool outPutBool;
string outPut;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
string test = getTest();
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://ip:port", "root", "password");
/* Connect to the MySQL test database */
con->setSchema("database name");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT `column name` FROM `table name` WHERE `test column` = '" + variable + "'"); //Variable is defined in the function input
while (res->next()) {
outPut = res->getString(1);
cout << outPut << endl;
cout << "Test\n"; //never runs
}
delete res;
delete stmt;
delete con;
}
catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
Sleep(10000); //Temporary delay so I can see if anything comes up before it runs another function
if (test != outPut)
doSomething();
else
doSomethingElse();
while 循环永远不会运行,我不知道为什么会发生这种情况,因为它似乎适用于很多其他人。我已经在连接器库中包含了所有库和头文件,但没有帮助。
在 phpmyadmin 中使用 SQL Query 函数可以正确显示输出,所以这不是查询的错。
如果有人可以在这里给我一些帮助,我将不胜感激,如果您有任何问题或需要更多我的代码,请提出。非常感谢您的帮助!