Mert Karabulut
Mert Karabulut

Reputation: 63

if (sqlite3_step(compiledStatement) == SQLITE_ROW) is fail

On the first implemetation file, all steps

- (void) readCitiesFromDatabase {

    databaseName = @"123.sqlite";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    // Setup the database object
    sqlite3 *database;

    // Setup the SQL Statement and compile it for faster access
    NSString *querySQL = [NSString stringWithFormat:@"SELECT City_Name, City_Picture, City_Id FROM CITIES, COUNTRIES WHERE CITIES.Country_Id=COUNTRIES.Country_Id AND COUNTRIES.Country_Id = '%@'", self.countryID];

    const char *sqlStatement = [querySQL UTF8String];
    sqlite3_stmt *compiledStatement;

    // Init the countries Array
    self.cities = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSLog(@"success");

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            NSLog(@"success");
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                NSLog(@"success");
                // Read the data from the result row
                char *temp_cName = (char *)sqlite3_column_text(compiledStatement, 0);
                NSString *cName = temp_cName == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cName];

                char *temp_cPicture = (char *)sqlite3_column_text(compiledStatement, 1);
                NSString *cPicture = temp_cPicture == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cPicture];

                char *temp_cId = (char *)sqlite3_column_text(compiledStatement, 2);
                NSString *cId = temp_cId == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cPicture];

                NSMutableDictionary *city = [[NSMutableDictionary alloc] init];
                [city setObject:cName forKey:@"City_Name"];
                [city setObject:cPicture forKey:@"City_Picture"];
                [city setObject:cId forKey:@"City_Id"];

                [self.cities addObject:city];
            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);   
    }
    sqlite3_close(database);
}

Now I got cities list. When I select a city, I could not see city info. This is from second implementation.

- (void) readCityInfoFromDatabase:(NSString *)querySQL
{
    databaseName = @"123.sqlite";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    // Setup the database object
    sqlite3 *database;

    // Setup the SQL Statement and compile it for faster access

    const char *sqlStatement = [querySQL UTF8String];
    sqlite3_stmt *compiledStatement;

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSLog(@"success");
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            NSLog(@"success");
            if (sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                NSLog(@"success");

                char *temp_content = (char *)sqlite3_column_text(compiledStatement, 0);
                [self.cityTextView setText:temp_content == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_content]];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

The first two steps are success but,

    if (sqlite3_step(compiledStatement) == SQLITE_ROW)
    {
        NSLog(@"success");

I couldn't see success on this step.

Please help.

Upvotes: 1

Views: 2396

Answers (1)

Rob
Rob

Reputation: 437592

You really should be saving the result of sqlite3_step() and checking to see if it is:

  • SQLITE_ROW = retrieved row of data
  • SQLITE_DONE = no (more) data to retrieve
  • Other = some error occurred

On the basis of what you shared, though, there is a good chance that you simply received SQLITE_DONE because there was nothing found. You could get that if there was a mistake in the SQL (e.g. bad join, incorrect WHERE clause, etc.). It's hard to say without seeing the SQL used by readCityInfoFromDatabase and some representative data from the database.

By the way, not only for sqlite3_step(), but rather for all of your sqlite3_xxx() calls, if you receive an error, you should be logging sqlite3_errmsg(). Right now, if there was an error, you'd have to guess what the source of the problem was.

Upvotes: 1

Related Questions