【问题标题】:custom object creation from database从数据库创建自定义对象
【发布时间】:2016-02-20 23:41:47
【问题描述】:

在我的项目中,我从 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 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 以及是否有办法纠正它

【问题讨论】:

    标签: c++ class import sqlite


    【解决方案1】:

    这个循环看起来不对:

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

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

    【讨论】:

    • 这是一个编码字符串副本,我在其他 3 个类中有相同的循环,它们复制得很好
    • 这是未定义的行为,因为您正在访问传递然后数组的末尾。您应该通过调试器运行您的代码,以了解发生了什么,因为我们没有数据库,因此我们无法产生您的错误。
    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多