HappyCoding
HappyCoding

Reputation: 15

How to fetch All data of the columns in the table

this is my code... i want to fetch all the quotes data that have 320 quote but it only fetch the first one quote. please help me

-(NSMutableArray *)getAllQuotesData
{
    NSMutableArray *quotesArray = [[NSMutableArray alloc] init];

    NSString *sqlStr = [NSString stringWithFormat:@"SELECT quote FROM quotes"];

    sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement:sqlStr];

    while (sqlite3_step(ReturnStatement)==SQLITE_ROW)
    {
        @try
        {
            QuotesDC *myQuote = [[QuotesDC alloc] init];

             NSString *user_id = [NSString stringWithUTF8String:(char 
*)sqlite3_column_text(ReturnStatement,0)];

             NSString *category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,1)];

             NSString *subcategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,2)];

             NSString *quote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,0)];

             NSString *star = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,4)];

            myQuote.userid = [user_id integerValue];
            myQuote.category = category;
            myQuote.subcategory = subcategory;
            myQuote.quote = quote;
            myQuote.star = star;

            [quotesArray addObject:myQuote];
            NSLog(@"%u", quotesArray.count);
        }
        @catch (NSException *ept) {
            NSLog(@"Exception in %s, Reason: %@", __PRETTY_FUNCTION__, [ept reason]);
        }

        return  quotesArray;
    }
}

Upvotes: 1

Views: 191

Answers (4)

Balu
Balu

Reputation: 8460

try like this once check your data base table 320 records placed or not if present then it'l gives ll the data.

SELECT quote FROM quotes  //it'l gives only quote columnvalues from quotes table.

if you want to get all the data then use asterisk '*' symbol in the place of quote.

otherwise try like this,

 SELECT quote FROM quotes limit 320 //it'l gives top 320 records

once read this one you'l found the problem. select Query

Upvotes: 0

Noor
Noor

Reputation: 2067

nothing wrong with your code just do one thing return dataArray will be outside of the loop , you will load all quotes like this, accept my answer thanks

-(NSMutableArray *)getAllQuotesData
{
    NSMutableArray *quotesArray = [[NSMutableArray alloc] init];

    NSString *sqlStr = [NSString stringWithFormat:@"SELECT quote FROM quotes"];

    sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement:sqlStr];

    while (sqlite3_step(ReturnStatement)==SQLITE_ROW)
    {
        @try
        {
            QuotesDC *myQuote = [[QuotesDC alloc] init];

             NSString *user_id = [NSString stringWithUTF8String:(char 
*)sqlite3_column_text(ReturnStatement,0)];

             NSString *category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,1)];

             NSString *subcategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,2)];

             NSString *quote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,0)];

             NSString *star = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,4)];

            myQuote.userid = [user_id integerValue];
            myQuote.category = category;
            myQuote.subcategory = subcategory;
            myQuote.quote = quote;
            myQuote.star = star;

            [quotesArray addObject:myQuote];
            NSLog(@"%u", quotesArray.count);
        }
        @catch (NSException *ept) {
            NSLog(@"Exception in %s, Reason: %@", __PRETTY_FUNCTION__, [ept reason]);
        }


    }
return  quotesArray;
}

Upvotes: 1

Nithinbemitk
Nithinbemitk

Reputation: 2730

Use this code,this may help you

-(void)retrivequoteFromDB
{
    NSMutableArray * quoteArray=[[NSMutableArray alloc]init];

    sqlite3 *sqliteDB;

    sqlite3_stmt *compiledStatement = NULL;

    if(sqlite3_open([databasePath UTF8String], &sqliteDB)==SQLITE_OK)
    {
        NSString *sql=@"SELECT quote FROM quotes";

        const char *sqlC=[sql UTF8String];

        if(sqlite3_prepare(sqliteDB, sqlC, -1, &compiledStatement, NULL)==SQLITE_OK)
        {
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                NSString *quote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                [quoteArray addObject:quote];
            }
        }
        sqlite3_reset(compiledStatement);
    }
    sqlite3_finalize(compiledStatement);
}

Upvotes: 0

Gaurav Rastogi
Gaurav Rastogi

Reputation: 2145

Replace your query string with this

 NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM quotes WHERE quote=320"];

Upvotes: 0

Related Questions