akk
akk

Reputation: 135

How to load uipickerview values from database in iphone?

I want to load data to uipickerview from database. this is my code

if (sqlite3_prepare_v2(UsersDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
int i=0;
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(@"select");
self.coun=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
 NSLog(@"Coun = %@",coun);

self.animals=[coun componentsSeparatedByString:@":"];
NSLog(@"String value=%@",self.animals);
} 
sqlite3_finalize(statement);
} else
{
NSLog(@"not select");
}

and button actions code is

- (IBAction)selectAnItem:(UIControl *)sender {
//Display the ActionSheetPicker
[ActionSheetPicker displayActionPickerWithView:sender data:self.animals selectedIndex:self.selectedIndex target:self action:@selector(itemWasSelected::) title:@"Select Country"];
}

but it run load only last value. Not all values loaded in UIpickerview. How to load all values from database using this code. Anyone pls help me. Thanks in advance.

Upvotes: 0

Views: 813

Answers (2)

karim
karim

Reputation: 15589

In the while loop, you have to collect all the self.coun values in an array. When you come out of the loop, having all the values, populate the picker.

NSMutableArray *countries = [[NSMutableArray alloc] init];


while (sqlite3_step(statement) == SQLITE_ROW){
    self.coun=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
    NSLog(@"Coun = %@",coun);
    [countries addObject:coun];    
    //self.animals=[coun componentsSeparatedByString:@":"];

    //NSLog(@"String value=%@",self.animals);
} 
// Now you can use countries to populate in picker.

You have to implement data source and delegates for the UIPickerView.

Seems, you are storing countries in DB, reading from DB you are assigning in self.animals. Thats kind of not matching things with variable names. And why are your parsing by ":"?

Upvotes: 0

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

You have to do like this,

NSArray *splitedString = [coun componentsSeparatedByString: @":"];
NSString *animalName;
if ([splitedString count]>0)
{
        animalName=[splitedString objectAtIndex:0];
        [self.animals addObject:animalName]; //here animals should be NSMutableArray 
}

Upvotes: 1

Related Questions