Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

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

It's me again probably asking noob C++ questions

I had MAJOR headaches making the darn (sorry for the language) MySQL C++ connector work. I don't know if it is poorly written or something, but for my experience yet I've never had so much trouble making something to work.

Anyhow I got it to connect and throw exceptions on failed connect/query which for me is quite big thing :U :P . The actual problem comes out of me obtaining the result of the query. Regardless of what I do my application always crashes :S

I used the 32-bit installer and the libmysql.dll/lib from the 32-bit MySQL server (since i'm compiling a 32-bit application i figured this is the right thing to do)

Here's some code so you could imagine what I'm talking about

DBManager.h

#ifndef DBMANAGER_H
#define DBMANAGER_H
#define CPPCONN_PUBLIC_FUNC
#define CPPCONN_LIB_BUILD True

#include <string>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

class DBManager
{
public:
    static DBManager* Instance();
    bool Query(const char* Query);
    void Connect(const char* DbHost, unsigned short DbPort, const char* DbUser, const char* DbPass, const char* DbName);
    bool ValidCredentials(const char* Username, const char* Password);
    void ManageException(sql::SQLException &e);

    ~DBManager();

protected:
    static DBManager* pInstance;

private:
    DBManager() {};
    DBManager(DBManager const&){};
    DBManager& operator=(DBManager const&){};

    sql::mysql::MySQL_Driver* driver;
    sql::Connection *Con;
    sql::PreparedStatement *pstmt;
    sql::ResultSet *res;
    sql::Statement *stmt;

    bool isConnected;
};

#endif

And now the cpp file DBManager.cpp

#include "DBManager.h"

DBManager* DBManager::pInstance = NULL;

DBManager* DBManager::Instance()
{
    if (!pInstance)
    {
        pInstance = new DBManager();
    }

    return pInstance;
}

bool DBManager::Query(const char* Query)
{
    return true;
}

DBManager::~DBManager()
{   
    delete Con;
    delete pstmt;
    delete res;
    delete stmt;
}

void DBManager::ManageException(sql::SQLException& e)
{
    if (e.getErrorCode() != 0) {
        std::cout << "# ERR: SQLException in " << __FILE__;
        std::cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl;
        std::cout << "# ERR: " << e.what();
        std::cout << " (MySQL error code: " << e.getErrorCode();
        std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl;
    }
}

void DBManager::Connect(const char* DbHost, unsigned short DbPort, const char* DbUser, const char* DbPass, const char* DbName)
{
    try {
        driver = sql::mysql::get_mysql_driver_instance();
        std::string connDSN = "tcp://" + std::string(DbHost) + ":3306";

        Con = driver->connect(connDSN, sql::SQLString(DbUser), sql::SQLString(DbPass));
        Con->setSchema(sql::SQLString(DbName));
        isConnected = true;

        std::cout<<"Database connection successul."<<std::endl;

    } catch(sql::SQLException &e) {
        ManageException(e);
        isConnected = false;

        return;
    }
}

bool DBManager::ValidCredentials(const char* Username, const char* Password)
{
    bool cred = false;

    try {
        pstmt = Con->prepareStatement("SELECT * FROM account WHERE account_name=? LIMIT 1"); // Smart use of indexing
        pstmt->setString(1, Username);
        res = pstmt->executeQuery();

        while(res->next())
        {
            if (res->getString("password") == Password)
            {
                cred = true;
            }
        }
    }
    catch(sql::SQLException &e) {
        ManageException(e);
        return false;
    }

    return cred;
}

Basically, It compiles without a problem, Connects without a problem, Executes queries without a problem, but the second I try to retrieve data some breakpoint exception is thrown in a file "xutils.cpp". I really have no idea what I'm doing wrong. I'm using the DEBUG libraries while compiling for debug. Hmm libmysql.dll should be release since I extracted it from the server bundle, but I don't seem to find it as a source to compile my own.

I really have no idea why it crashes and burn like that :/

PS: Don't mind the no hashing of the password, it really is just a proof of concept to me in the way of ... getting it to work first, then securing it :U

PS: I also have Boost libraries compiled and ready in the project, if that would help :U

EDIT: Main function

bool ServerRunning = true;
int main(int argc, char** argv)
{
    #ifdef _WIN32
        std::string title = TEXT("Window Title Change");
        SetConsoleTitle(title.c_str());
    #endif;

    std::cout<<"Loading Configuration File..."<<std::endl<<std::endl;

    std::string path = boost::filesystem::path(boost::filesystem::current_path()).string();
    path += "\Config.ini";

    INIParser* Config = new INIParser(path.c_str()); //MinINI

    // Sockets data
    std::string listenIP = Config->GetString("Network", "ListenIP", "127.0.0.1");
    unsigned short listenPort = Config->GetInt("Network", "ListenPort", 5000);

    // Database data
    std::string dbHost = Config->GetString("Database", "Host", "localhost");
    std::string dbUser = Config->GetString("Database", "User", "root");
    std::string dbPass = Config->GetString("Database", "Password", "");
    std::string dbName = Config->GetString("Database", "Database", "authserv");
    unsigned short dbPort = Config->GetInt("Database", "Post", 1000);

    // General settings
    int sessionTimeout = Config->GetInt("Settings", "SessionTimeout", 10);
    int maxClients = Config->GetInt("Settings", "MaxClients", 10);
    int serverTimeout = Config->GetInt("Settings", "GameserverTimeout", 1);

     // Begin Initialization
     DBManager::Instance()->Connect(dbHost.c_str(), dbPort, dbUser.c_str(), dbPass.c_str(), dbName.c_str());
     bool loginSuccess = DBManager::Instance()->ValidCredentials("Username", "Password");

    char c;
    while (ServerRunning)
    {
        std::cin>>c;

        if (c == 'q')
        {
            ServerRunning = false;
        }
    }

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...