从数据库创建自定义对象

custom object creation from database

本文关键字:对象 自定义 创建 数据库      更新时间:2023-10-16

在我的项目中,我从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] = '';
}

我不会对您代码的任何其他部分发表评论。