COUNT (*) gives EXC_BAD_ACCESS
While Executing SQLITE with SELECT statement and having COUNT (*) to count the total number of that column, gives me EXC_BAD_ACCESS. It sends me the error while adding an array from one array to other. No idea why it does that but has anybody come across such issues can help around ?
-(void) title{
self.array = [[[NSMutableArray alloc] init] autorelease];
const char *query_stmt = "SELECT DISTINCT ID, KEY, COUNT (*) FROM TEST GROUP BY KEY";
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
NSString *ID = [NSString stringWithUTF8String:(char *) sqlite3_column_text(exhProduct_statement, 0)];
NSString *KEY = [NSString stringWithUTF8String:(char *) sqlite3_column_text(exhProduct_statement, 1)];
NSUInteger taskCount= sqlite3_co开发者_Python百科lumn_int(exhProduct_statement, 2);
NSMutableArray *taskArray=[[NSMutableArray alloc]initWithObjects:ID, KEY, taskCount,nil] ; <--- Error
[self.array addObject:taskArray];
}
sqlite3_finalize(statement);
}
}
You need to change the NSUInteger
into an NSNumber
:
NSMutableArray *taskArray=[[NSMutableArray alloc]initWithObjects:ID, KEY, [NSNumber numberWithUnsignedInteger:taskCount],nil] ;
You can later retrieve the value using
NSUInteger *retrievedTaskCount = [[taskArray objectAtIndex:2] unsignedIntegerValue];
The reason for this is that an NSArray
stores NSObject
s, so any time that you have an int
, float
, NSInteger
, NSUInteger
, etc, that you wish to store in an NSArray
or NSMutableArray
, you will need to convert it into an object. NSNumber
is a class build just for this.
精彩评论