Reputation: 2067
I want to fetch data from sqlite db in objective-c in the form of listArray(like in java android).
Here i have java class models for this
How can i convert these classes into objective c code confused in deceleration(.h) and implementation(.m)
Class 1
public class RecordingList {
public ArrayList<RecordingItem> Items = new ArrayList<RecordingItem>();
}
class 2
public class RecordingItem {
public int ID;
public String RecordingTitle;
public String RecordingSubTitle;
public String RecordingAudioPath;
public String RecordingDuration;
public String RecordingNotePath;
public String Date;
public String Time;
public String PlayListId;
public ArrayList<RecordingAttachments> recordingAttachmentses = new ArrayList<RecordingAttachments>();
}
I have try to get data in objective-c like this
- (NSArray*) getAllNotes
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"SELECT * FROM recordingItem"];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:name];
NSString *department = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
[resultArray addObject:department];
NSString *year = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[resultArray addObject:year];
return resultArray;
}
else{
NSLog(@"Not found");
return nil;
}
sqlite3_reset(statement);
}
}
return nil;
}
But this is not what i want i want to get data in list of objects how can i do this?
Thanks in Advance
Upvotes: 0
Views: 1302
Reputation: 4528
First off it is advisable to start with Objective C basics on your own. After that you'll be able to rewrite your Java classes in a couple of minutes.
What about data fetching, if you don't want to use CoreData (which is recommended and handy, if you target only iOS), then you can use eg. FMDatabase which is easier to use and hides all the native stuff you wrote here.
One more notice: you put name
, department
, year
one after another in an array, create an ObjC @interface
which will hold all the data:
@interface Data : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *department;
@property (nonatomic, strong) NSString *year;
@end
and use it like that:
NSMutableArray *resultArray = [NSMutableArray new];
// fetch ...
Data *data = [[Data alloc] init];
data.name = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
// other fields ...
[resultArray addObject:data];
One more notice: you can now create parametric arrays just like in Java.
Upvotes: 1