MySQL C++ Connector crashes my app at ResultSet->getString()

陌路散爱 提交于 2019-11-29 15:35:16

Assuming the password field is defined as varchar in the database, you cannot use getString() to retrieve it. You must instead use the blob function, getBlob().

This is how the while loop would look:

while(res->next())
{
    std::istream * retrievedPassword_stream = res->getBlob("password");
    if (retrievedPassword_stream)
    {
        char pws[PASSWORD_LENGTH+1]; // PASSWORD_LENGTH defined elsewhere; or use other functions to retrieve it
        retrievedPassword_stream->getline(pws, PASSWORD_LENGTH);
        std::string retrievedPassword(pws); // also, should handle case where Password length > PASSWORD_LENGTH
        if (retrievedPassword == std::string(Password))
        {
            cred = true;
        }
    }
}

Side comments: Note that there are some other issues with the code.

  • The statement handle must be deleted, so you should do a delete pstmt; at the appropriate place in the ValidCredentials() function (rather than in the destructor). (But, why use a prepared statement in that case anyways? Better to initialize the prepared statement in the constructor (or somewhere else outside the function the query is called), as well as delete in the destructor or elsewhere, if you do use a prepared statement. Instead of a prepared statement, though, note that prepared statements are most useful for very high-use and high-CPU intensive queries, so using it for password validation might not be important here (you could just execute a regular query, instead of a prepared statement).)
  • Likewise, the ResultSet needs to be deleted (delete res) at the end of the try block, rather than in the destructor.
  • Be sure to check for NULL before using pstmt, res, or Con.
  • stmt appears to be unused and should not be deleted.
helloyangqi
  1. download mysql c++ connector
  2. compile mysqlcppconn-static project use mt or mtd
  3. your project add CPPCONN_LIB_BUILD
  4. your project add (2) built static library
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!