kumar
kumar

Reputation: 453

how to retrieve the multiple columns data from the database?

I am new to this database programming.i retrieved single column data from the database.but i am unable to retrieve multiple columns data from the database.

here is my code

-(void) readItemsFromDatabaseforTable:(NSString *)tableName {


    arrayItems = [[NSMutableArray alloc] init];


    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
 {

           NSString *sql_str = [NSString stringWithFormat:@"select * from %@", tableName];

        const char *sqlStatement = (char *)[sql_str UTF8String];

            NSLog(@"query %s",sqlStatement);

        sqlite3_stmt *compiledStatement;

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
{

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

                NSString *idsstr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];               

                [arrayItems addObject:idsstr];
                NSLog(@"*************** %@",arrayItems);
            }
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{

          NSString *caloriesstr = [NSString stringWithUTF8String:  

(char*)sqlite3_column_text(compiledStatement, 4)];               

                [caloriesarry addObject:caloriesstr];

                NSLog(@"naveenkumartesting");

                 NSLog(@"*************** %@",caloriesarry);

}

}

my main aim is to retrieve columns data from the database and store into multiple arrays.

please guys anyone help me,

Upvotes: 0

Views: 1560

Answers (2)

Nitin
Nitin

Reputation: 7461

Use this sample Query and make your query according to your requirements..

NSString *str = [NSString stringWithFormat:@"Select * from Yourtablename where image1 = '%@' AND person1 = '%@' AND category = '%@' AND PurchaseAmt = '%@'",Str,person1,category,Amountdata];

Use this tutorial

Upvotes: 1

Vishal Kardode
Vishal Kardode

Reputation: 971

You dont need to loop with while a second time when you can get all data in a single while as so:

const char *stmt = "select id, name from table1 where isImage = 1";
sqlite3_stmt *selectstmt;
if (sqlite3_prepare_v2(database, stmt, -1, &selectstmt, NULL) == SQLITE_OK) {
    while(sqlite3_step(selectstmt) == SQLITE_ROW) {
        int rowid = sqlite3_column_int(selectstmt, 0);
        NSString * name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];

    }
}

Upvotes: 2

Related Questions