iPhone and iPad : Doing a "select * from something" query in a SQLite database
- by Abramodj
Hi folks, i'm trying to use the SQLite data base in my iPad app, and here's my function to make a query:
- (void)executeQuery:(char*)query
{
NSString *file = [self getWritableDBPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:file];
// If its not a local copy set it to the bundle copy
if(!success) {
//file = [[NSBundle mainBundle] pathForResource:DATABASE_TITLE ofType:@"db"];
[self createEditableCopyOfDatabaseIfNeeded];
}
dataArray = NULL;
dataArray = [NSMutableArray array];
sqlite3 *database = NULL;
if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) {
sqlite3_exec(database, query, loadTimesCallback, dataArray, NULL);
}
sqlite3_close(database);
[self logResults];
}
if I call [self executeQuery:"select name from table1"]; everything is working fine. But if i call [self executeQuery:"select * from cars"]; the app crashes telling me that the NSMutableArray dataArray is not the right kind of variable where to set the query results.
So, how can i do a "select * form table1" query, and store the results?
Thanks!
EDIT: Here's my loadTimesCallback method:
static int loadTimesCallback(void *context, int count, char **values, char **columns)
{
NSMutableArray *times = (NSMutableArray *)context;
for (int i=0; i < count; i++) {
const char *nameCString = values[i];
[times addObject:[NSString stringWithUTF8String:nameCString]];
}
return SQLITE_OK;
}