I am trying to have an entire column of my sqlite database returned and placed into an array, but am having much difficulty getting it done. The query string itself works fine as I have tested it against my database in a database managing app. They code that I am using works fine in returning an array of a row, because I have used it elsewhere in my app many times.
Code:
NSMutableArray *locationList = [[NSMutableArray alloc] init];
const char *sql = [[NSString stringWithFormat:@"SELECT ColumnName FROM TableName"] UTF8String];
NSString *file = [[NSBundle mainBundle] pathForResource:@"databaseName" ofType:@"sqlite"];
sqlite3 *database = NULL;
if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) {
sqlite3_exec(database, sql, MyCallback, locationList, NULL);
}
sqlite3_close(database);
also here is the function "MyCallback"
Code:
static int MyCallback(void *context, int count, char **values, char **columns)
{
NSMutableArray *names = (NSMutableArray *)context;
for (int i=0; i < count; i++) {
const char *nameCString = values[i];
[names addObject:[NSString stringWithUTF8String:nameCString]];
}
return SQLITE_OK;
}
I have a feeling it might have to do with this function, but I am not entirely sure what it does. :/ I placed it there based on some tutorial I followed a while ago.
I am self taught, so I am not so great at understanding documentation, but any resources you can give me that might help would definitely be appreciated.
Thanks in advance all who are much more knowledgeable than I!