2015-11-19 52 views
1

在我的项目中,我从sqlite3数据库中提取数据并使用检索到的数据创建自定义对象。我没有任何问题与2的对象,他们创造完美,但这第三个我有问题。 SQL表有3列,CD,曲目,标题从数据库中创建自定义对象

从数据库中提取数据来创建一个对象:

 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(); 
} 

问题我” ()被调用时,我得到这个作为文件中的输出:

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

并从那里继续。

我想知道为什么track_id总是零和标题始终是1,如果有一种方法来纠正它

回答

0

这个循环如下错误:

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

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

+0

这是一个编码字符串副本,我在其他3个类中有相同的循环,他们复制精细 –

+0

它是未定义的行为,因为您正在访问您的数组通过然后结束。您应该通过调试器运行您的代码,以了解发生了什么,因为我们没有数据库,所以我们无法产生错误。 –