lundi 4 avril 2016

IOS Objective C SQLite data to tableview

Im trying to get data from my sqlite database to a table view but cannot get my head around the different types of arrays and dictionaries

I current populate a table view with the following code

- (void)viewDidLoad {
[super viewDidLoad];
// Initialize table data
recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;}

The wrapper I use for sql creates a NS MutableDictionary by using the following code

- (void)viewDidLoad {
[super viewDidLoad];

FASQLiteDB *fasqlitedb = [FASQLiteDB sharedInstance];
[fasqlitedb setupDatabaseWithDBFile:@"vistorlog"];
int nn = 5;
recipes2 = [fasqlitedb executeSelectQuery2:@"SELECT * FROM LOG"];
 NSLog(@"records : %@",recipes2);


-(NSArray *)executeSelectQuery2:(NSString *)query
{
if (self.isdbConnected == YES) //Check if database is connected
{
    sqlite3_stmt *statement = NULL;
    const char *sql = [query UTF8String];
    if (sqlite3_prepare_v2(sqliteDatabase, sql, -1, &statement, NULL) == SQLITE_OK)
    {

        NSMutableArray *columnNames = [[NSMutableArray alloc] init];
        NSMutableArray *columnTypes = [[NSMutableArray alloc] init];

        BOOL requireFetchColumnInfo = YES;
        int columnCount = 0;

        NSMutableArray *records = [[NSMutableArray alloc] init];

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            if (requireFetchColumnInfo)
            {
                columnCount = sqlite3_column_count(statement);

                for (int index = 0; index < columnCount; index++)
                {
                    NSString *columnName = [NSString stringWithUTF8String: sqlite3_column_name(statement, index)];
                    [columnNames addObject: columnName];
                    [columnTypes addObject: [NSNumber numberWithInt: [self columnTypeAtIndex: index inStatement: statement]]];
                }


                requireFetchColumnInfo = NO;
            }

            NSMutableDictionary *record = [[NSMutableDictionary alloc]init];
            for (int index = 0; index < columnCount; index++)
            {
                id value = [self columnValueAtIndex: index withColumnType: [[columnTypes objectAtIndex: index] intValue] inStatement: statement];
                if (value != nil)
                {
                    [record setValue: value forKey: [columnNames objectAtIndex: index]];
                }
            }

            [records addObject: record];
        }

        return records;

    }
    else
    {
        NSLog(@"[SQLITE] Error when preparing fucking query: %s", sqlite3_errmsg(sqliteDatabase));
        sqlite3_finalize(statement);

    }

}

return nil;

}

the NSLog of recipes2 is as follows:

{

    CATEGORY = Staff;

    COMPANY = ess;

    DBS = Yes;

    NAME = "Mark";

    TIMEIN = "25/05/2016 20:00";

    TIMEOUT = "25/05/2016 21:00";

    "_ID" = 1;

},

    {

    CATEGORY = Contractor;

    COMPANY = "ess";

    DBS = YES;

    NAME = "Mark";

    TIMEIN = "04-04-2016 00:00";

    TIMEOUT = "<null>";

    "_ID" = 2;

},

    {

    CATEGORY = Contractor;

    COMPANY = ess;

    DBS = YES;

    NAME = Rebecca;

    TIMEIN = "04-04-2016 00:03";

    TIMEOUT = "<null>";

    "_ID" = 3;

},

    {

    CATEGORY = Staff;

    COMPANY = "";

    DBS = YES;

    NAME = "";

    TIMEIN = "04-04-2016 00:04";

    TIMEOUT = "<null>";

    "_ID" = 4;

},

    {

    CATEGORY = Staff;

    COMPANY = "";

    DBS = YES;

    NAME = "";

    TIMEIN = "04-04-2016 00:05";

    TIMEOUT = "<null>";

    "_ID" = 5;

},

    {

    CATEGORY = Visitor;

    COMPANY = "ESS";

    DBS = YES;

    NAME = "Mark";

    TIMEIN = "04-04-2016 00:06";

    TIMEOUT = "04-04-2016 08:11";

    "_ID" = 6;

}

)

How do do modify the tableview code so that it put each of the 6 objects from the database on a line of the table

Your help is really appreciated

Mark

Aucun commentaire:

Enregistrer un commentaire