jeudi 22 octobre 2015

Assigning an "object" from one table to another "object" in another table from a SQLite Database

So I'm currently working on a NavControl project that consists of multiple Companies and Multiple Products associated with that company. So when you click on a company like "Apple" it will push you to a 'child' view controller that has "Apple Products" like iPhone, iPad, etc.

Originally i had a bunch of objects of type Company and Product (my two classes) all in a Data Access Object Class. These are all called in my parent view controller in an array called self.companylist.

So what I've done now is make a database with SQLite browser with all of those companies and products within it (two tables in one database).

So far I've been able to retrieve my Companies. That was the easy part. Now retrieving the Products associated to said Company is proving to be difficult.

My Product table from the database has a Primary Key(id) as well as a foreign Key that references the Company Primary Key(id).

My question is how can I assign those products to the Company using that foreign key somehow?

The method i am posting is checking to see if a database exists in the Doc Directory.. if its there use this database. Else Copy the database into Doc Directory from my Resources Bundle.

If i someone out the part where i am SELECT * FROM Product everything is fine so I'm just rally struggling on how to assign the products to the company.

-(void)findOrCopyDB{

    self.arrayOfCompAndProd = [[NSMutableArray alloc] init];
    [self.arrayOfCompAndProd removeAllObjects];

    NSString *dbName = @"NavControllerDataBase.db";

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docPath = [path objectAtIndex:0];

    self.dbPath =[[NSString alloc] initWithString:[docPath stringByAppendingPathComponent:dbName]];

    NSLog(@"%@",self.dbPath);

    NSFileManager *fileManager = [NSFileManager defaultManager];



    sqlite3_stmt *statement;

    // Checking if database exists in Doc Directory

    if ([fileManager fileExistsAtPath:self.dbPath])
    {
        const char *dbPath = [self.dbPath UTF8String];

        if(sqlite3_open(dbPath, &companyListDB) == SQLITE_OK)
        {

            NSLog(@"Database opened successfully!!");

            NSString *selectDB = [NSString stringWithFormat:@"SELECT * FROM Company"];
            const char *query_sql = [selectDB UTF8String];

            if (sqlite3_prepare(companyListDB, query_sql, -1, &statement, NULL) == SQLITE_OK)
            {
                while (sqlite3_step(statement)== SQLITE_ROW)
                {
                    NSString *name = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
                    NSString *logo = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 2)];
                    NSString *stock_code = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 3)];

                    Company *comp = [[Company alloc] init];
                    comp.companyName = name;
                    comp.companyLogo = logo;
                    comp.stockCode = stock_code;

                    [self.arrayOfCompAndProd addObject:comp];

                    NSLog(@" %@", self.arrayOfCompAndProd);

                    self.companyList = self.arrayOfCompAndProd;

                }

            }

//start

            NSString *selectDB_Two = [NSString stringWithFormat:@"SELECT * FROM Product"];
            const char *query_sql_Two = [selectDB_Two UTF8String];

            if (sqlite3_prepare(companyListDB, query_sql_Two, -1, &statement, NULL) == SQLITE_OK)
            {
                while (sqlite3_step(statement)== SQLITE_ROW)
                {
                    NSString *p_name = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
                    NSString *p_logo = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 2)];
                    NSString *p_url = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 3)];

                    Product *prod = [[Product alloc] init];
                    prod.productName = p_name;
                    prod.productLogo = p_logo;
                    prod.productURL = p_url;



//                    [self.arrayOfCompAndProd addObject:prod];
//                    [self.companyList addObjectsFromArray:self.arrayOfCompAndProd];
//                    self.companyList = self.arrayOfCompAndProd;

                    /*---------------------------------------------------------------*/
//Assign products to company.. Loop through company..
                    for (int i = 0; i < self.companyList.count; i++) {


                    }

                   /*---------------------------------------------------------------*/

                }

            }

//end
        }
        sqlite3_close(companyListDB);

        }

    else
    {

        //else copy databased from bundle to Doc Directory
        NSString *appDBBundlePath = [[NSBundle mainBundle] pathForResource:@"NavControllerDataBase" ofType:@"db"];
        NSLog(@"App Bundle Path - %@",appDBBundlePath);
        NSError *error;
        [[NSFileManager defaultManager] copyItemAtPath:appDBBundlePath toPath:self.dbPath error:&error];

        if(error) {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"ok"
                                                  otherButtonTitles:nil];
            [alert show];
        }


    }


}

Aucun commentaire:

Enregistrer un commentaire