从数据库创建自定义对象

时间:2015-11-19 01:15:36

标签: c++ class import sqlite

在我的项目中,我从sqlite3数据库中提取数据并使用检索到的数据创建自定义对象。我对2个对象没有任何问题,它们创造了完美,但第三个我遇到了问题。 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 table\n");
            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 found\n";
        error << "cd_id = " << cd_id << "\n";
        error << "track_id = " << track_id << "\n";
        error << "title = " << title << "\n\n\n";
        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 = %d\n", cd_id);
    printf("track_id = %d\n", track_id);
    printf("title = %s\n", 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] = '\0';
    }
    title = foo;
    printf("sanity check in track constructor\n");
    printf("title = %s\n", title);
    this->report();
}

我遇到的问题是调用report()时,我将其作为文件中的输出:

cd_id = 1
track_id = 0
title = 1
-------------------------------
cd_id = 2
track_id = 0
title = 1
-------------------------------

并且从那里继续。

我想知道为什么track_id总是为零,标题总是1,如果有办法纠正它

1 个答案:

答案 0 :(得分:0)

这个循环看起来不对:

for (int i = 0; i < temp.length(); i++){
    foo[i] = temp.c_str()[i];
    foo[i + 1] = '\0';
}

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