使用 MySQL 1.0.5 的 C++ 堆错误

c++ heap error using mysql 1.0.5

本文关键字:C++ 错误 MySQL 使用      更新时间:2023-10-16

我正在用 c++ 测试 mysql,并加载一个包含 4 列的简单表并将它们存储在映射中。我收到一个堆错误HEAP[mysql.exe]: Invalid address specified to RtlFreeHeap( 0E510000, 002C7238 ).

//=================================
// include guard
#pragma once
//=================================
// forward declared dependencies
//class Foo;
//class Bar;
//=================================
// included dependencies
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
//=================================
// the actual class
class DBO{
protected:
    //getters & setter variables
    std::string Table;
    std::map<std::string,std::string> Fields;
    //internal uses
    std::string UserName;
    std::string Password;
    std::string DB; 
    std::stringstream rowcount;
    sql::Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;
    sql::ResultSet *res;
    sql::ResultSetMetaData *res_meta;
public:
    //Constructors
    DBO(void){}
    DBO(std::string TableVal){
        Table = TableVal;       
    }
    void connect(){
        UserName = "myuser";
        Password = "mypass";
        DB = "mydb";
        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", UserName, Password);
    }
    void close(){
        delete res;
        delete stmt;
        delete con;
    }
    void test(){        
        try {   
            connect();
            con->setSchema(DB);
            stmt = con->createStatement();
            res = stmt->executeQuery("SELECT * FROM " + Table + " LIMIT 1");
            res_meta = res->getMetaData();
            for(int i=1;i<=res_meta->getColumnCount();i++){
                _cout(res_meta->getColumnName(i));
                //_cout(res_meta->getColumnType(i));
                Fields.insert( std::pair<std::string,std::string>(res_meta->getColumnName(i),"test") );
            }         
            while (res->next()) {
                for (std::map<std::string,std::string>::iterator it=Fields.begin(); it!=Fields.end(); ++it){
                    Fields[it->first] = res->getString(it->first); //this throws error
                    //it->second = res->getString(it->first); this throws error also
                    _cout(it->first+": "+it->second);
                }
            }           
        } catch (sql::SQLException &e) {
            _cout("#ERR: SQLException in ");
            _cout(e.what());
            _cout(e.getSQLState());
        }
        close();
    }
    void _cout(std::string out){
        dbPrint(const_cast<char *>(out.c_str()));
    }
};//END OF CLASS

编辑:我更改了消息,因为它与运行时无关。我发现该错误是使用电子邮件地址约为 20 个字符的 varchar 字段引发的。在进一步测试之后,似乎任何具有 15 个字符或更多字符的字段都会引发错误。我试图研究各种可能性,听起来它可能无法分配足够的内存。如果不是内存问题,我将如何分配更多内存或解决问题?感谢您的帮助。

由于列在数据库中定义为varchar列,因此cannot use getString()检索它。您必须改用blob function, getBlob()

你可以参考这个getblob()来获取varchar列