Hi All I have a class that works with sqlite database in my application. Here you can see one of the functions that I have write. This function must get the count of items in witch the column value is equals to the given value.
+ (int) GetCountOfItems: (NSString*) byColumn {
// Autorelease Pool.
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init];
// Create Sqlite query string.
NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT COUNT(*) FROM [Articles] WHERE %@ = 1", byColumn];
NSLog(@"GetCountOfItems query string is: %@", sqliteQuery);
// Create statement.
sqlite3_stmt* stmt;
int articleCount = 0;
if( sqlite3_prepare_v2(database, [sqliteQuery UTF8String], -1, &stmt, NULL) == SQLITE_OK ) {
if( sqlite3_step(stmt) == SQLITE_ROW )
articleCount = sqlite3_column_int(stmt, 0);
}
else NSLog(@"Failed from GetCountOfItems. Error is: %c", sqlite3_errmsg(database));
// Finalize.
sqlite3_finalize(stmt);
// Release Pool.
[pool release];
return articleCount;
}
I want to know if this function is correct for example should i use NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init]; ???? And how it can help me with memory ?
Based on the following description (in the Memory Management Programming Guide):
I’d say don’t bother, just allocate any objects as required, then release them before returning from the method.
Also, see this post.