【问题标题】:SQLITE crash when no data present in table表中没有数据时 SQLITE 崩溃
【发布时间】:2011-01-27 08:29:56
【问题描述】:

我的应用程序出现问题,当使用表格视图时表格中没有数据时导致应用程序崩溃。我已经测试了我的代码,只要存在数据,它就可以正常工作,但是当没有数据存在时,我需要它才能工作。

-(void)initialiseTableData
{
NSMutableArray *array = [[NSMutableArray alloc]init];

sqlite3 *db = [iCaddyAppDelegate getNewDBConnection];
sqlite3_stmt  *statement;
const char *sql = "select courseId, courseName, totalPar, totalyardage, holePars,        holeYardages, holeStrokeIndexs from Course";
if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!= SQLITE_OK) 
{
  NSAssert1(0,@"Error preparing statement",sqlite3_errmsg(db));
  sqlite3_close(db);
}

else
{

while (sqlite3_step(statement) == SQLITE_ROW) 
{

  Course *temp = [[Course alloc]init];

  temp.courseId = sqlite3_column_int(statement,0);
  temp.courseName = [NSString     stringWithFormat:@"%s",(char*)sqlite3_column_text(statement,1)];
  temp.totalPar =sqlite3_column_int(statement,2);
  temp.totalYardage =sqlite3_column_int(statement,3);

  NSString *tempHolePars = [NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement,4)]; 
  NSString *tempHoleYardages = [NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement,5)];
  NSString *tempHoleStrokeIndexes = [NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement,6)];

  NSArray *temp1 = [tempHolePars componentsSeparatedByString:@":"];
  NSArray *temp2 = [tempHoleYardages componentsSeparatedByString:@":"];
  NSArray *temp3 = [tempHoleStrokeIndexes componentsSeparatedByString:@":"];

  for(int i = 0; i<=17; i++)
  {
    NSString *temp1String = [temp1 objectAtIndex:i];
    [temp.holePars insertObject:temp1String atIndex:i];

    NSString *temp2String = [temp2 objectAtIndex:i];
    [temp.holeYardages insertObject:temp2String atIndex:i];

    NSString *temp3String = [temp3 objectAtIndex:i];
    [temp.holeStrokeIndexes insertObject:temp3String atIndex:i];
  }

  [array addObject:temp];
} 

  self.list = array;
  [self.table reloadData];

}



}

【问题讨论】:

    标签: iphone objective-c sqlite


    【解决方案1】:

    可能是您的数据库未正确打开。

    sqlite3 *db = [iCaddyAppDelegate getNewDBConnection];

    重写getNewDBConnection

    
    -(NSInteger)getNewDBConnection:(sqlite3 *db)database {
        return sqlite3_open([self charPathToDB], &database);
    }
    

    然后可以检查数据库是否正确打开

    
    sqlite3 *db;
    NSInteger rc;
    
    

    rc = [iCaddyAppDelegate getNewDBConnection:&db];

    if (rc) { NSAssert1(0,@"Error opening database: %s",sqlite3_errmsg(db)); sqlite3_close(db); return; }

    如果你想为你的 sqlite 数据库提供一个好的包装器,你还应该查看FMDB

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多