Использование необъявленного идентификатора ‘SQLStatement’

#ios #objective-c #sqlite #xcode5

#iOS #objective-c #sqlite #xcode5

Вопрос:

Привет, я очень новичок в разработке iOS, я использую код учебника вот он: http://hubpages.com/hub/IOS-5-How-To-Display-SQLite-Data-in-a-UITableView # но я изменяю его так, как он должен поступать со мной. но иногда я получаю какую-то ошибку, хотя в учебнике и у меня одинаковые коды. вот код из учебников:

 -(NSMutableArray *) authorList{
    theauthors = [[NSMutableArray alloc] initWithCapacity:10];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"AuthorsDb.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], amp;db) == SQLITE_OK))
        {
            NSLog(@"An error has occured: %@", sqlite3_errmsg(db));    
        }
        const char *sql = "SELECT * FROM  books";
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, amp;sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
        }else{

            while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
                  Author * author = [[Author alloc] init];
                author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
                author.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
                author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
                [theauthors addObject:author];
            }
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
    }
    @finally {
    sqlite3_finalize(sqlStatement);
        sqlite3_close(db);

        return theauthors;
    }
}
  

и вот мой код:

 -(NSMutableArray *) Servicelist{
    theservices = [[NSMutableArray alloc] initWithCapacity:10];
    @try {

        // CHANGE THE NAME OF THE DATABASE
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"DBManicurious.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], amp;db) == SQLITE_OK))
        {
            NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
        }
        //ALTER THE SELECTION

        const char *sql = "SELECT ServiceName, Price FROM tbl_Services WHERE TitleID=1"; //the Table view will be populated by the type ID
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, amp;sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
        }else{
            while (sqlite3_step(sqlStatement)==SQLITE_ROW) {

                //This one must be altered .. :)
                /*must recode this as soon as you get this -->>*/
                services1 *services = [[services1 alloc] init];
                services.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
                // This one is an integer so find a way and recode this as you get this
                services.price = sqlite3_column_int(sqlStatement, 1);
                // author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
                [theservices addObject:services];
            }
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
    }
    @finally {
        sqlite3_finalize(sqlStatement);
        sqlite3_close(db);
        return theservices;
    }
}
  

Я получаю сообщение об ошибке в «sqlite3_finalize (SQLStatement);»
в нем говорится: Использование необъявленного идентификатора ‘SQLStatement’

Ответ №1:

Вы объявили

 sqlite3_stmt *sqlStatement;
  

внутри @try блока, чтобы он был там. Переместите объявление за пределы @try и в качестве бонуса инициализируйте его равным нулю, например

 sqlite3_stmt *sqlStatement = 0;
@try { ...