从数据库创建自定义对象
custom object creation from database
在我的项目中,我从sqlite3数据库中提取数据,并使用检索到的数据创建自定义对象。我对其中的两个对象没有任何问题,它们创造了完美,但第三个我有问题。sql表有3列,cd、track、title
从数据库中提取数据以创建对象:
while (1)
{
res = sqlite3_step(statement);
if (res == SQLITE_ROW)
{
/*
// read info into data sections
cd_id = (char*)sqlite3_column_text(statement, 0);
track_id = (char*)sqlite3_column_text(statement, 1);
title = (char*)sqlite3_column_text(statement, 2);
*/
// create cd object
track temp((char*)sqlite3_column_text(statement, 0), (char*)sqlite3_column_text(statement, 1), (char*)sqlite3_column_text(statement, 2));
// insert into map if it passes integrity test
if (temp.getCD(atoi(cd_id), &cd_map) != NULL){
std::pair<int, int> key(atoi(cd_id), atoi(track_id));
track_map.insert(std::pair<std::pair<int, int>, track>(key, temp));
}
}
if (res == SQLITE_DONE || res == SQLITE_ERROR)
{
printf("done with track tablen");
break;
}
} // end of while
} // end of if prepare
包括构造函数的对象函数:
cd *track::getCD(int cdID, std::map<int, cd> *foo){
std::map<int, cd>::iterator iter = (*foo).find(cdID);
if (iter != (*foo).end()){ // found
return &(*iter).second;
}
else // not found
{
std::ofstream error;
error.open("exceptions.txt", std::ios::app);
error << "exception thrown:n" << "insert track - cd not foundn";
error << "cd_id = " << cd_id << "n";
error << "track_id = " << track_id << "n";
error << "title = " << title << "nnn";
error.close();
return NULL;
}
}
void track::report(){ // report to file
std::ofstream output;
output.open("track.p2.report.txt", std::ios::app);
// open output file in append mode
output << "cd_id = " << cd_id << "n";
output << "track_id = " << track_id << "n";
output << "title = " << title << "n";
output << "-------------------------------n";
output.close();
}
void track::print(){
printf("cd_id = %dn", cd_id);
printf("track_id = %dn", track_id);
printf("title = %sn", title);
printf("________________________n");
}
//// constructor ////
track::track(char *cd, char *track, char *ttl){
cd_id = atoi(cd);
track_id = atoi(track);
std::string temp(ttl);
char *foo = new char[temp.length() + 1];
for (int i = 0; i < temp.length(); i++){
foo[i] = temp.c_str()[i];
foo[i + 1] = ' ';
}
title = foo;
printf("sanity check in track constructorn");
printf("title = %sn", title);
this->report();
}
我遇到的问题是,当调用report()时,我会将其作为文件中的输出:
cd_id = 1
track_id = 0
title = 1
-------------------------------
cd_id = 2
track_id = 0
title = 1
-------------------------------
并且从那里继续。
我想知道为什么track_id总是零,title总是1,以及是否有方法纠正它
这个循环看起来不对:
for (int i = 0; i < temp.length(); i++){
foo[i] = temp.c_str()[i];
foo[i + 1] = ' ';
}
我不会对您代码的任何其他部分发表评论。
相关文章:
- 自定义对象的dlib序列化在gcc中失败
- 如何将带有自定义对象的容器从C++传递到QML
- 将 exprtk 与自定义类的对象一起使用
- 将 RTOS 队列对象封装在仅具有静态分配的 IQueue 自定义接口中
- 如何实现自定义匹配器以检查 Catch2 中的对象相等性
- 如何使用Q_PROPERTY公开自定义对象列表
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 从自定义类获取对象向量中的 max 元素
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- 如何在自定义对象的<<运算符中添加自定义前缀
- 如何使用 STL 排序对具有模板专用化的自定义类对象进行排序?
- 如何从 cpp 中的函数返回自定义类对象?
- 如何为自定义模板对象创建专门的函数模板
- 带有自定义deleter的std::unique_ptr对象的大小(一个由ref捕获的lambda)
- 基于3个对象的自定义排序
- 如何键入用于const对象的自定义io操纵器
- 指向临时对象的自定义迭代器(延迟加载)
- 将自定义对象作为参数从目标 C 传递到 C++
- 从 Arduino 中的自定义C++库构造对象时出错
- 从链表访问对象(自定义实现)