开发者

SQLITE crash when no data present in table

Im having a problem with my app that causes it to crash when no data is present in the table when using a table view. I have tested my code and it works fine as long as there is data present but i need it to work when there is no data present.

-(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];开发者_如何学JAVA

}



}


Likely that your db is not opened properly.

sqlite3 *db = [iCaddyAppDelegate getNewDBConnection];

Re-write getNewDBConnection


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

Then you can check that the database was opened correctly


sqlite3 *db;
NSInteger rc;

rc = [iCaddyAppDelegate getNewDBConnection:&db];

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

You should also check out FMDB if you want a good wrapper for your sqlite database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜