如何在C++中使用MySQL lib

问题描述 投票:0回答:1

当我运行 C++ 代码来检查某些内容时,我总是收到错误“内存分配失败:分配错误”,但用户名和密码是正确的。

我只是在主函数中调用该函数,因此它从程序开始,receedLicense 只是一个我将检查的字符串

MySQL 库:https://dev.mysql.com/downloads/connector/cpp/

bool IsLicenseValid(const std::string& receivedLicense) {
    try {
        bool isValid = false;

        sql::mysql::MySQL_Driver* driver;
        sql::Connection* con;

        // Create a MySQL connection
        driver = sql::mysql::get_mysql_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "root", "");

        // Connect to the specific database
        con->setSchema("social");

        // Execute a simple query to check if the received license is in the database
        sql::Statement* stmt = con->createStatement();
        sql::ResultSet* res = stmt->executeQuery("SELECT `owner_unigID` FROM server_license WHERE license = '" + receivedLicense + "'");

        // Check if the license is valid based on the query result and Check if the owner exists
        if (res->next()) {
            std::string owner = res->getString("owner_unigID");

            // Clean up resources for the first query
            delete res;
            delete stmt;

            // Execute a simple query to check if the owner exists
            stmt = con->createStatement();
            res = stmt->executeQuery("SELECT `uniqid` FROM users WHERE uniqid = '" + owner + "'");

            // Check if the license is valid based on the query result
            isValid = res->next();
        }

        // Clean up resources
        delete res;
        delete stmt;
        delete con;

        return isValid;
    }
    catch (sql::SQLException& e) {
        std::cerr << "MySQL Error: " << e.what() << std::endl;
        return false;
    }
    catch (const std::bad_alloc& e) {
        std::cerr << "Memory allocation failure: " << e.what() << std::endl;
    }
}

我尝试使用 MySQL 库中的文档并在互联网上搜索问题

c++ mysql visual-studio runtime-error
1个回答
0
投票

只使用Release而不是Debug xD

© www.soinside.com 2019 - 2024. All rights reserved.