0

Iam trying to read data from a table. Initially it is empty. If i tried to read at that time it will cause an exception. My code is given Below

-(NSMutableArray *) selectDataFrom:(NSString *) tableName
{   
    NSString *qsql = [NSString stringWithFormat:@"SELECT * FROM '%@' ",tableName];  
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2( dataBase, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK)
    {
        NSLog(@"INSIDE IF");                    
        while (sqlite3_step(statement) == SQLITE_ROW) 
        {
            NSLog(@"INSIDE WHILE");         
            // my code
        }
    }
    return allData; 
}   

The first NSLog("INDISE IF"); is printed. But the second one is not printing.

Mention some books to learn sqlite3 statements of iPhone [ eg: sqlite_prepare_v2();] not SQL

[ Sorry for my poor English]

THIS IS THE FULL CODE

-(NSMutableArray *) selectDataFrom:(NSString *) tableName
{

    allData = [[NSMutableArray alloc] init];
    NSString *qsql = [NSString stringWithFormat:@"SELECT * FROM '%@' ",tableName];
    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2( dataBase, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK)
    {       


        while (sqlite3_step(statement) == SQLITE_ROW) 
        {
            NSLog(@"REACHED 1");            
            myProgram = [[Programs alloc] init];    

            char *date  = (char *) sqlite3_column_text(statement, 0);
            myProgram.nss_Date = [[NSString alloc] initWithUTF8String:date];            

            char *type  = (char *) sqlite3_column_text(statement, 3);
            myProgram.nss_Type = [[NSString alloc]initWithUTF8String:type];

            [nsma_allDonorsMA addObject:d_OneDonor];        
        }
    }
    else {
        //      NSLog(@"failed to select data");

    }
    return allData;
}
4
  • This is the exception: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array' *** Call stack at first throw: Commented Sep 9, 2010 at 23:37
  • What happens where you typed // my code? Since you’re not doing anything with an NSMutableArray in the code you showed we can’t say what you did wrong with it. Commented Sep 9, 2010 at 23:44
  • Sven, The problem is when selecting an empty table. When i tried to read a non-empty table , there is no problem. Commented Sep 11, 2010 at 16:47
  • Thank you Sven. for your precious time Commented Sep 11, 2010 at 17:07

2 Answers 2

1

Try enabled breakpoints on exceptions and run your app in the debugger. It will show you exactly where you are calling objectAtIndex:. (It is not in the above code)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you St3fan, I did as you told. I found the reason for exception.
0

Where is your sqlite3_finalize() ??

You should release sqlite3_stmt structure in your sqlite3_prepare_v2 block, as following.

sqlite3_stmt *sql_stmt; 
if (sqlite3_prepare_v2(...) == SQLITE_OK)
{
    ....

    sqlite3_finalize(sql_stmt);
}

1 Comment

Thank you Toro for your reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.