ytpm
ytpm

Reputation: 5150

SQLite3 statement SELECT, WHERE and ORDER BY on Obj-C

I have a problem with SQLite3 on Xcode. the problem is that I have this database:

-------------------------|
| ID | Company  |  Model |
|------------------------|
| 1  | Audi     |  A4    |
| 2  | Audi     |  A4    |
| 3  | Audi     |  A3    |
| 4  | Audi     |  A4    |
| 5  | BMW      |  330Ci |
| 6  | BMW      |  330Ci |
| 7  | BMW      |  750i  |
| 8  | Mercedes |  CL65  |
| 9  | Mercedes |  CL65  |
--------------------------

I want the sqlite3 to show me like this:

-------------------------|
| ID | Company  |  Model |
|------------------------|
| 1  | Audi     |  A4    |
| 2  | Audi     |  A3    |
| 3  | BMW      |  330Ci |
| 4  | BMW      |  750i  |
| 5  | Mercedes |  CL65  |
--------------------------

I have this function:

-(NSString *)loadModel
{
    thecars = [[NSString alloc]initWithFormat:@"The data has been loaded to the pickers."];
    sqlite3_stmt * sqlStatement;
    @try {
        NSString *dbPath = [self path];
        if(sqlite3_open([[self path]UTF8String], &db) == SQLITE_OK)
        {
            NSLog(@"**** Database found at path.");
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"**** An error has occured: %@", sqlite3_errmsg(db));
        }
        NSString * hello = [NSString stringWithFormat:@"SELECT DISTINCT Model FROM Comp WHERE Company=%@",theChoosenCompany];
        const char * sql = [hello UTF8String];
        if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"**** Problem with prepare statement: %@", sqlite3_errmsg(db));
        }
        else
        {
            while (sqlite3_step(sqlStatement) == SQLITE_ROW) {
                NSString * model = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement, 2)]; // Car models
                [modelsArray addObject:model];
            }
        }
    }
    @catch (NSException *exception) {
        NSLog(@"**** Problem with prepare statement:  %@", sqlite3_errmsg(db));
    }
    @finally {
        sqlite3_finalize(sqlStatement);
        sqlite3_close(db);
        return thecars;
    }
}

the problem is that I always gets an error and the program crush with error "Thread 1: Program received signal: "EXC_BAD_ACCESS"".

I believe that the stament is wrong.

Please if someone can help, i'm new to objective-c so please be patient.

Thanks alot!

Upvotes: 2

Views: 1843

Answers (2)

Rob
Rob

Reputation: 437917

Personally, I'd start stepping through the code line-by-line in a debugger and see precisely what line is causing the problem.

As an aside, I don't see where you're setting theChoosenCompany. Is that properly quoted and stuff like that?

Also, if you want unique combinations of Model & Company, isn't it just

SELECT Model, Company FROM Comp GROUP BY Model, Company

Obviously I don't know your logic, as you said above you wanted just the unique Model/Company combinations, but your code seems to try to retrieve unique models for a given company.

Finally, there are Objective C wrappers for SQLite3, such as FMDB (at https://github.com/ccgus/fmdb), that get you out of the weeds of sqlite3 calls for stuff like this.

Upvotes: 1

borrrden
borrrden

Reputation: 33421

I'm fairly green on SQLite myself, but if you select only the models, wouldn't it be column 0 and not column 2 in this line: NSString * model = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement, 2)]; // Car models ?

Upvotes: 1

Related Questions