skip to Main Content

I am trying to read data from a database using the 8.0.13 MySQL C++ Connector. I am able to successfully write to a database no problem, but when I try to get the results of the database (using result next) it never runs.

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 << "Testn"; //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();

The while loop never runs and I am clueless to why this happens as it seems to work for a lot of other people. I have included all libraries and headers in the connector library, but to no help.

Using the SQL Query function in phpmyadmin properly displays the output, so it’s not the query’s fault.

I would greatly appreciate it if anyone could give me some help here, and if you have any questions or need more of my code just ask. Thanks a lot for the help!

2

Answers


  1. I’m not sure how, but after just simply adding a cout statement between the query and the while loop it suddenly solved itself. I removed the cout and now it’s works perfectly no problem. Not sure what caused this error, but I’m happy that it solved itself after I have been trying to fix it for quite a while!

    Login or Signup to reply.
  2. When you concatenate an SQL statement dynamically and when it then does not return the results you are expecting, it is very often that the generated SQL statement is not as you expected to be.

    It’s hard to tell what’s wrong, because we cannot reproduce it without your DBMS, of course.
    But usually one will write the SQL statement to stdout, copy it to an interactive SQL console then and see what happens:

    std::string query = "SELECT `column name` FROM `table name` WHERE `test column` = '" + variable + "'";
    std::cout << query << std::endl;  // open console and copy/paste it to your DBMS 
    res = stmt->executeQuery(query); //Variable is defined in the function input
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search