0
votes

in my project im using a database.There are almost 365 questions in that.so i want to get a particulat question for a particular day.i used the code below to fetch a qusestion from database.

-(void)print{
    sqlite3_stmt *statement;


   // SELECT * from light where rowid = %i",1
        qsql=[NSString stringWithFormat:@"Select * from ishh where col_1 = '365'"];


        if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
            NSLog(@"%dsssssssssssssss",sqlite3_step(statement));
            NSLog(@"%ddddddddddddddddddddd", (SQLITE_ROW));
            NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            NSLog(@"%@ gg",Q_NO);

when i use the above code it is printing the correct question from database.Also when i give the statement like qsql=[NSString stringWithFormat:@"Select * from ishh where col_1 = '1'"]; it is not fetching the question. But when i use the below code it is not fetching from the database.

-(void)print{
    sqlite3_stmt *statement;

   // SELECT * from light where rowid = %i",1
        qsql=[NSString stringWithFormat:@"Select * from ishh where col_1 = '1'"];


        if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
            NSLog(@"%dsssssssssssssss",sqlite3_step(statement));
            NSLog(@"%ddddddddddddddddddddd", (SQLITE_ROW));
            NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            NSLog(@"%@ gg",Q_NO);
            while (sqlite3_step(statement) == (SQLITE_ROW))
            { 
                NSLog(@" iolo");
                 NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
                //NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%i",sqlite3_column_int(statement, 0)]];

                NSLog(@"%@ gg",Q_NO);


            }  
            sqlite3_reset(statement);

        }
    sqlite3_finalize(statement);
    }

             Can anyone tell me where im going wrong.Thanks in advance.
1
Are you sure you have a record in ishh where col_1 is 1? If it's working on '365' and not on '1' that seems to be the problem.alex-i
yes..there is a record in thatishhhh
What type of column is col_1? If it's not INTEGER make sure it doesn't have any spaces. Can you also check if sqlite returns any errors? (add an else with NSLog(@"%s", sqlite3_errmsg(database));)alex-i
NSLog(@"%s", sqlite3_errmsg(database)); this is not printing.i think it is entering in "if loop" so i wont print na.Also how can we check the whether spaces are there in database?can you help meishhhh
please post everything you get in the consolealex-i

1 Answers

0
votes
-(void)print{
    sqlite3_stmt *statement;
    qsql=[NSString stringWithFormat:@"Select * from ishh where col_1 = '1'"];

    if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
        while (sqlite3_step(statement) == (SQLITE_ROW))
        { 
            char *field0 = (char *)sqlite3_column_text(statement, 0);
            char *field1 = (char *)sqlite3_column_text(statement, 1);
            NSString *Q_NO = nil;
            if(field0!=NULL){
                Q_NO = [[NSString alloc] initWithUTF8String:field0];
            }
            NSString *Q_NO1 = nil;
            if(field1!=NULL){
                Q_NO1 = [[NSString alloc] initWithUTF8String:field1];
            }
            NSLog(@"%@ %@",Q_NO, Q_NO1);

            [Q_NO release];
            [Q_NO1 release];
        }  
    }
    sqlite3_finalize(statement);
}