简体   繁体   中英

iPhone + sqlite3 + fmdb, What code do i need to put data into a UiTableView datasource?

If I was adding data from an array to the UITableView datasource array I'd use this, in viewDidLoad.

NSMutableArray *array = [[NSArray alloc] initWithObjects:@"Head First Design Patterns", @"Head First HTML & CSS", @"Head First iPhone", nil];
self.transactionsArray = array;
[array release];

And this in cellForRowAtIndexPath

NSInteger row = [indexPath row];
cell.textLabel.text = [transactionsArray objectAtIndex:row];

But I want to link up the results from a select query, I'm using fmdb to access my database. Heres how I output data to the console with fmdb at the moment.

FMDatabase* db = [FMDatabase databaseWithPath:@"/tmp/mydb.db"];
if (![db open]) {
    NSLog(@"Could not open db.");
}

FMResultSet *rs = [db executeQuery:@"select * from myTable",  nil];
while ([rs next]) {
    NSLog(@"%@, %@, %@, %@, %@", [rs stringForColumn:@"pid"],
            [rs stringForColumn:@"desc"], 
            [rs stringForColumn:@"due"],
            [rs stringForColumn:@"price"],
            [rs stringForColumn:@"accumulated_price"]);
}
[rs close]; 
[db close];

How do i do this ?

Build a transaction array in the while loop like this:

[transactionArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[rs stringForColumn@"pid", @"dictionaryLabel",........, nil];

EDIT: Yup thats right, to accesses it just use:

descLabelInTableView = [[arrayTmp objectAtIndex:indexPath.row] objectForKey:@"desc"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM