使用 MySQL 1.0.5 的 C++ 堆错误
c++ heap error using mysql 1.0.5
我正在用 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列
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'