how can I insert values of sqlite3 database table into NSMutableArray? - iphone

I have retrieved data from sqlite3 database table as below
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *price = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
now I have an NSMutableArray named as Data, I have declared and synthesized it properly, now how can I add values into it, I tried addObject, and print in NSLog, but showing null value, while there are values in name and price?
help me out

If the array is nil, it will never accept values because it doesn't exist. Even if you synthesize a property, it doesn't mean the property gets a default object assigned to it. Somebody still has to make sure an instance of NSMutableArray exists.

NSNumber *WId;
int temp1 = (int)sqlite3_column_int(walkstatement, 0);
WId = [[NSNumber alloc] initWithInt:temp1];
char *WNameCharacter;
WNameCharacter = (char *) sqlite3_column_text(walkstatement, 1);
NSString *WNameString = [[NSString alloc] initWithUTF8String:WNameCharacter];
NSMutableDictionary *tempWalk = [[NSMutableDictionary alloc] init];
[tempWalk setObject:WId forKey:#"WalkId"];
[tempWalk setObject:WNameString forKey:#"WalkName"];
[regionWalkArray addObject:tempWalk];
This worked for me please change the variables according to your requirement..

Related

get object from [myarray objectAtIndex:indexPath.row] instead of NSString

i have a custom class called contact. This class has attributes like name, address etc.
I wish to create an array of type contact and display the value of each attibute in a custom cell in UItableview.
Code to store the data in array:
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
myContact.nm = aName;
NSString *aAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
myContact.address = aAddress;
NSLog(#"%#",aName);
[list.details addObject:myContact];
NSLog(#"%#", list.details);
}
here details is an array declared as #property(nonatomic,retain)NSMutableArray *details; in .h
My question is that how do i retrieve the values on each index like :
[0]->name
[0]->address
[1]->name
[1]->address ....
my code in tableview.m:
cell.name.text = [details objectAtIndex:indexPath.row]; // not working
how do i solve this?
First make an object of class.
Contact *con = [details objectAtIndex:indexPath.row];
It will store all array in 'con' object.
Then, Use it by,
NSLog("Name :: %#", con.nm);
NSLog("Addr :: %#", con.address);
It'll refer Name & Address of that particular contact object.
Thanks.
[[details objectAtIndex:indexPath.row] nm];
In nm you just need to input your object. This works for me.
[details objectAtIndex:indexPath.row]; returns the Contact object. What you need is, nm property of Contact object which represents the name in your code.
Try this,
Contact *myContacts = [details objectAtIndex:indexPath.row];
cell.name.text = [myContacts nm]; //or [[details objectAtIndex:indexPath.row] nm];
Similarly you can get [myContacts address] as well.
Edit:
Based on your question in comments, the answer is:
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
Contact *myContact = [[Contact alloc] init]; //you should add this here, not outside the loop
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
myContact.nm = aName;
NSString *aAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
myContact.address = aAddress;
NSLog(#"%#",aName);
[list.details addObject:myContact];
NSLog(#"%#", list.details);
}
Well, are you looking for
cell.name.text = [[details objectAtIndex:indexPath.row] name];
?
You need:
cell.name.text = [[details objectAtIndex:indexPath.row] nm];
When you use this line:
[details objectAtIndex:indexPath.row];
It returns a object of type contact. Then you need to get the nm property which stores the name.
So you need to use like:
[[details objectAtIndex:indexPath.row] nm];

Having problems with Array

SO here's my setup. I have an object called radiostations where I have several strings like callsign, frequency declared and an NSMutableArray called amStationInfo. On my viewcontroller, I access an SQLite database which populates the an array like so...
radiostations.h
#interface radiostations : NSObject {
NSString *format;
NSString *city;
}
#property (nonatomic, retain) NSString *format;
#property (nonatomic, retain) NSString *city;
ViewController.m
radiostations *amStationClass = [[radiostations alloc] init];
NSMutableArray* amStationInfo = [[NSMutableArray alloc] init];
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *cityField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 10)];
NSString *formatField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[amStationInfo addObject:amStationClass];
[amStationClass setCity:cityField];
[amStationClass setFormat:formatField];
}
[tabView reloadData];
sqlite3_finalize(statement);
and then I populate a UITableView
NSString *cityValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] city];
NSString *formatValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] format];
cityLabel.text = cityValue;
formatLabel.text = formatValue;
Initially I was dealing with a few Arrays and this worked just fine. I then changed it so that I was only dealing with one array using a class object and now it's not working. I know the SQLite query and what not works so Im not having any problems with that. It seems as though the array does not get populated.
You are changing the properties of the same radiostations object and adding it over and over again to the array. You need to create a new radiostations object for each row from your sqlite database and add this:
while (...) {
// fetch data as before
radiostations *record = [[radiostations alloc] init];
[record setCity: cityField];
[record setFormat: formatField];
[amStationInfo addObject: record];
[record release];
}
If you are using ARC you need to remove the line [record release];, otherwise it is necessary to avoid leaking those objects.
where did you allocate/init your mutablearray?
something like:
NSMutableArray* amStationInfo = [[NSMutableArray alloc] init];
you need to allocate it once, before to add objects in it

How to get ordered values from an unordered NSDictionary

I am trying to fetch and display contents from table.Currently i am running a query and store those id, and category name using NSMutableDictionary which works fine.
But I face one issue when i store the contents into NSMutableDictionary the order gets changed.When searching for this issue , it seems that this is the default behavior of the NSMutableDictionary.
I am not sure and how to use and what use to store the key pair value.
Please advice me that how can i overcome this problem
Thanks for stopping by...
//====================================================================
- ( NSMutableDictionary * ) getDataToDisplayTierOne:(NSString*)dbPath{
//====================================================================
NSMutableDictionary *aTierOneTemplateData = [[NSMutableDictionary alloc]init];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql_query_stmt = "select * from main_categories";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSString *aValue = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 1)];
NSString *aId = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 0)];
[aTierOneTemplateData setObject:aId forKey:aValue];
[aValue release];
[aId release];
}
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
return aTierOneTemplateData;
}
Note : - I understand the limitations on using the nsmutabledictionery
so i expecting your advice to that how can i overcome / fetch id,
value in other way.please suggest
Updated
For example
Apple 1
Orange 2
Papaya 3
Actually i want that list to display it on my uitableview
Apple
Orange
Papaya
When tapping on each cell , i want to have its id value
for instance , i tap on Orange , now i want to have that id value as 2
Can you please tell me how to iterate over array of dictionery
Use NSMutableArray instead of NSMutableDictionary.
Change the declaration
NSMutableDictionary *aTierOneTemplateData = [[NSMutableDictionary alloc]init];
to
NSMutableArray *aTierOneTemplateList = [[NSMutableArray alloc]init];
And add the abjects to the array,
[aTierOneTemplateList addObject:aId];
And return the NSMutableArray,
return aTierOneTemplateList;
Edit:
If you modify the while loop as follows, you will get array of NSDictionarys with the same order.
NSString *aValue = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 1)];
NSString *aId = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 0)];
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:aValue,#"value",aId,#"key", nil];
[aTierOneTemplateList addObject:dict];
[dict release];
[aValue release];
[aId release];
Edit2:
Consider now in your tableviewcontroller, you have array of NSDictionarys (say aTierOneTemplateList).
In cellForRowAtIndexPath method,
NSDictionary *dict = [aTierOneTemplateList objectAtIndex:indexPath.row];
NSString *key = [dict objectForKey:#"key"];
NSString *value = [dict objectForKey:#"value"];
Similarly in didSelectRowAtIndexPath method.
Neither NSDictionary nor NSSet are ordered collections. If you want ordering, use NSArray. If you want to impose ordering, add objects to unordered collections that have their own index attribute.

how to assign string array values to variable

what i am doing is sorting the labels for that i am assigning the string array to temporary string variable as follows:
NSString *tmp = [labels objectAtIndex:j-1];
labels[j-1]=labels[j];
labels[j] = tm;
but it shows error at last line that is incomplete types in assignment
how can i solve this?
thank you
Labels is not a C array, it's and Objective-C array, so you need to do
NSMutableArray *labels2 = [[NSMutableArray alloc] initWithArray:labels];
NSString *tmp = [labels objectAtIndex:j-1];
[labels2 insertObject:[labels objectAtIndex:j] atIndex:j-1];
[labels2 insertObject:tm atIndex:j];

obj-c problem setting array with componentsSeperatedByString

I have a data source with about 2000 lines that look like the following:
6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A
What I am interested in is the 6th section of this string so I want to turn it into an array, then i want to check the 6th section [5] for an occurrance of that string "PAKP"
Code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *airportsPath = [bundle pathForResource:#"airports" ofType:#"dat"];
NSData *data = [NSData dataWithContentsOfFile:airportsPath];
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSArray *dataArray = [dataString componentsSeparatedByString:#"\n"];
NSRange locationOfAirport;
NSString *workingString = [[NSString alloc]initWithFormat:#""];
NSString *searchedAirport = [[NSString alloc]initWithFormat:#""];
NSString *airportData = [[NSString alloc]initWithFormat:#""];
int d;
for (d=0; d < [dataArray count]; d=d+1) {
workingString = [dataArray objectAtIndex:d];
testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:#","];
testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
testTextBox3 = [workingArray objectAtIndex:1] //throws exception index beyond bounds
locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:#"PAKP"];
}
the problem is that when the workingArray populates, it only populates with a single object (the first component of the string which is "6712". If i have it display the workingString, it correctly displays the entire string, but for some reason, it isn't correctly making the array using the commas.
i tried it without using the data file and it worked fine, so the problem comes from how I am importing the data.
ideas?
You code works. You should run it with the debugger to see what's happening. At a guess, your input data isn't what you think it is - possibly a different encoding, or different line endings.
See sample:
NSString *dataString = #"6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A";
NSArray *dataArray = [dataString componentsSeparatedByString:#"\n"];
for (NSString *workingString in dataArray) {
NSString *testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:#","];
NSString *testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
NSString *testTextBox3 = [workingArray objectAtIndex:1]; //throws exception index beyond bounds
NSRange locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:#"PAKP"];
}
there was a problem in the data where there were a few "\"s that caused the errors.