//find all data from database
- (void) findAllSupplement
{
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: #"SELECT * FROM SupplementInformationTable"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSMutableArray *allRows = [[NSMutableArray alloc] init] ;
while(sqlite3_step(statement) == SQLITE_ROW)
{
char *field1 = (char *) sqlite3_column_text(statement,0);
NSString *field1Str1 = [[NSString alloc] initWithUTF8String: field1];
char *field2 = (char *) sqlite3_column_text(statement,1);
NSString *field1Str2 = [[NSString alloc] initWithUTF8String: field2];
char *field3 = (char *) sqlite3_column_text(statement,2);
NSString *field1Str3 = [[NSString alloc] initWithUTF8String: field3];
char *field4 = (char *) sqlite3_column_text(statement,3);
NSString *field1Str4= [[NSString alloc] initWithUTF8String: field4];
char *field5 = (char *) sqlite3_column_text(statement,4);
NSString *field1Str5 = [[NSString alloc] initWithUTF8String: field5];
char *field6 = (char *) sqlite3_column_text(statement,5);
NSString *field1Str6 = [[NSString alloc] initWithUTF8String: field6];
char *field7 = (char *) sqlite3_column_text(statement,6);
NSString *field1Str7= [[NSString alloc] initWithUTF8String: field7];
char *field8 = (char *) sqlite3_column_text(statement,7);
NSString *field1Str8 = [[NSString alloc] initWithUTF8String: field8];
char *field9 = (char *) sqlite3_column_text(statement,8);
NSString *field1Str9 = [[NSString alloc] initWithUTF8String: field9];
NSMutableDictionary *dataDictionary=[[NSMutableDictionary alloc]init];
[dataDictionary setObject:field1Str1 forKey:#"supplementIdentity"];
[dataDictionary setObject:field1Str2 forKey:#"supplementName"];
[dataDictionary setObject:field1Str3 forKey:#"supplementDescription"];
[dataDictionary setObject:field1Str4 forKey:#"offerIdentity"];
[dataDictionary setObject:field1Str5 forKey:#"offerName"];
[dataDictionary setObject:field1Str6 forKey:#"offerDescription"];
[dataDictionary setObject:field1Str7 forKey:#"flavourIdentity"];
[dataDictionary setObject:field1Str8 forKey:#"imageLink"];
[dataDictionary setObject:field1Str9 forKey:#"quantityPurchase"];
// Add the string in the array.
[allRows addObject:dataDictionary];
NSLog(#"Match found") ;
//send all data to the cart container screen
CartContainer *objCartContainer=[[CartContainer alloc]init];
objCartContainer.arrayConatinLocalDataFromDB=allRows;
}
sqlite3_finalize(statement);
NSLog(#"%#",allRows);
}
sqlite3_close(_contactDB);
}
}
how i can call this function in app delegate ?this function is declared and define in view controller named productDetail.m .And i want to execute this function for my entire app. help will be appreciated.
Write the function in Appdelegate.m
And call from viewController.m
form this code:
AppDelegate *appDelegate;
appDelegate= (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate findAllSupplement];
Or you can declare the funtion globally. like:
In ViewController.h
+ (void) findAllSupplement;
And Define the funtion in ViewControler.m
+ (void) findAllSupplement
{
// your code here
}
And you can call the funtion from any class.
Related
I am storing multiple values in Sqlite database in my iOS app.
I need to retrieve the values under "cellForRowAtIndexPath"
- (NSString *) retrieveDetailText :(NSString *) emailAddress :(NSString *) uidText :(NSString *) folderName {
NSString *detailText = nil;
const char *dbpath = [databasePath UTF8String];
//SQLIte Statement
NSString *selettablequery = [NSString stringWithFormat:#"select * from MailFolderDBTable"];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
if (sqlite3_prepare(database, [selettablequery UTF8String], -1, &statement, NULL) ==SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
char *uidfield = (char *) sqlite3_column_text(statement, 5);
NSString *uidStr = [[NSString alloc] initWithUTF8String: uidfield];
NSLog(#"retrieveDetailText: uidStr: %#", uidStr);
if([uidStr isEqualToString:uidText]) {
char *emailstring = (char *) sqlite3_column_text(statement, 6);
NSString *detailTextData = [[NSString alloc] initWithUTF8String: emailstring];
NSData *data = [[NSData alloc] initWithBase64EncodedString:detailTextData options:NSDataBase64DecodingIgnoreUnknownCharacters];
detailText = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
From this code, I need to loop through at while(sqlite3_step(statement) == SQLITE_ROW)
As I am calling retrieveDetailText under cellForRowAtIndexPath, and it is not looping through here while(sqlite3_step(statement) == SQLITE_ROW), It checks if([uidStr isEqualToString:uidText]) only one time.
How can i loop through while(sqlite3_step(statement) until all the rows checked and get the value.
If you only need one row which satisfies uidText, then you can change your query no?
//SQLIte Statement
NSString *selettablequery = [NSString stringWithFormat:#"select * from MailFolderDBTable where uid = %#", uidText];
Otherwise, you can fetch all the data before loading your tableView, in an array and then use it as your tableView data source.
HERE IS MY CODE:
NSString *ask = [NSString stringWithFormat:#"SELECT name FROM users"];
sqlite3_stmt *statement;
const char *query_statement = [ask UTF8String];
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
{
if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
}
sqlite3_finalize(statement);
sqlite3_close(_DB);
}
}
I want to set those result above to NSMUtableArray like this example:
totalStrings =[[NSMutableArray alloc]initWithObjects:#"xxx",#"xxx",#"xxx", nil];
What should I do ?
=================UP DATE ALL CODE =======================
Here is the aditonal code to make it easy to see. I really donĀ“t know what I have done wrong.
#interface ViewController ()
{
NSMutableArray *totalStrings;
NSMutableArray *filteredStrings;
BOOL isFiltered;
}
#property(strong, nonatomic)NSMutableArray *entries;
#end
#implementation ViewController;
#synthesize entries;
- (void)viewDidLoad {
// totalStrings =[[NSMutableArray alloc]initWithObjects:#"ggg",#"gggggg", nil];
// NSLog(#"%#",totalStrings);
NSMutableArray* result=[[NSMutableArray alloc] init];
NSString *ask = [NSString stringWithFormat:#"SELECT * FROM users"];
sqlite3_stmt *statement;
const char *query_statement = [ask UTF8String];
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
{
if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
//add your namefield here.
[result addObject:nameField];
totalStrings=[[NSMutableArray alloc] initWithArray:result];
}
sqlite3_finalize(statement);
sqlite3_close(_DB);
}
}
NSLog(#"%#",totalStrings);
//AND PUT THOSE RESULTS INTO THIS ARRAY ( I WILL USE THIS LATER)
//I want it to be this format: totalStrings=[[NSMutableArray alloc]initWithObjects:#"test1",#"test2",#"test3", nil];
}
The code below should work in case you want to get all namefields in array
NSMutableArray* result=[[NSMutableArray alloc] init];
NSString *ask = [NSString stringWithFormat:#"SELECT name FROM users"];
sqlite3_stmt *statement;
const char *query_statement = [ask UTF8String];
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
{
if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
//add your namefield here.
[result addObject:nameField];
}
sqlite3_finalize(statement);
sqlite3_close(_DB);
}
}
In the end you can do
totalStrings=[[NSMutableArray alloc] initWithArray:result];
Create and Alloc New NSMutableArray totalStrings and add nameField in the totalStrings in While Loop.
NSMutableArray *totalStrings = [[NSMutableArray alloc] init];
In while loop
[totalStrings addObject:nameField];
You can get it like this,
-(NSArray *)list_totalStrings
{
NSMutableArray *totalStrings = [[NSMutableArray alloc] init];
NSString *ask = [NSString stringWithFormat:#"SELECT name FROM users"]; //Make sure field and table names are proper
sqlite3_stmt *statement;
const char *query_statement = [ask UTF8String];
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
[totalStrings addObject:nameField];
}
sqlite3_finalize(statement);
sqlite3_close(_DB);
NSLog(#"%#",totalStrings);
return totalStrings;
}
else
return nil;
}
else
return nil;
}
i am Newbie in iOS Development, and First time Use Sqllite database so Sorry For this Question but i am So Confusing Please Give me Solution for this.
I added Data in to My Sqllite Table Like as
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:
#"INSERT INTO ALLREADTABLE (PostId, PostTitle, ImageLink, ShortDescription,Category,PostDate) VALUES (\"%#\", \"%#\", \"%#\", \"%#\",\"%#\",\"%#\")",
post_id, cell.headLabel.text, image,self.shortDiscription,cell.categoryLabel.text,cell.timeLabel.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_myDatabase, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSString *string=[NSString stringWithFormat:#"Value Added %# %# %# %# %# %#",post_id, cell.headLabel.text, image,self.shortDiscription,cell.categoryLabel.text,cell.timeLabel.text];
NSLog(#"String %#",string);
} else
{
NSLog(#"Failed to add contact");
}
sqlite3_finalize(statement);
sqlite3_close(_myDatabase);
}
Then it is added data in to Data Base as I want and it also added Duplicate Value in to table , so For Distinct Value i Write a Code to Fetch data like as
- (void)getTextFomDB
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
#"sampleDatabase.db"]];
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK)
{
NSString *querySQL = #"SELECT DISTINCT PostId, PostTitle, ImageLink, ShortDescription,Category,PostDate FROM ALLREADTABLE";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
[self.postId removeAllObjects];
[self.postTitle removeAllObjects];
[self.imageLink removeAllObjects];
[self.shortDecsription removeAllObjects];
[self.category removeAllObjects];
[self.postDate removeAllObjects];
while (sqlite3_step(statement) == SQLITE_ROW) {
//NSString *personID = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)];
NSString *postId = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)];
NSString *postTitle = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 2)];
NSString *ImageLink = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 3)];
NSString *shortDescrip = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 4)];
NSString *Category = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 5)];
NSString *postDate = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 6)];
[self.postId addObject:[NSString stringWithFormat:#"%# ", postId]];
[self.postTitle addObject:[NSString stringWithFormat:#"%# ",postTitle]];
[self.imageLink addObject:[NSString stringWithFormat:#"%#",ImageLink]];
[self.shortDecsription addObject:[NSString stringWithFormat:#" %#",shortDescrip]];
[self.category addObject:[NSString stringWithFormat:#" %#",Category]];
[self.postDate addObject:[NSString stringWithFormat:#" %#",postDate]];
}
sqlite3_finalize(statement);
}
sqlite3_close(_myDatabase);
}
}
Then it Give me Error like as
[NSPlaceholderString initWithUTF8String:]: NULL cString
Here I want Distinct Value From My SqlliteTable. please Give me Solution For that. I know It is easy But i am Newbie in Database so please Sorry
and Thanks in advance.
You are using initWithUTF8String to get values from database, So if there is a field in database which accept null data than it will gives you an error when you are trying to fetch null data from database.
I suggest you replace all data retrieving code like
NSString *postId = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)];
with below code:
NSString *postId = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
This will replace not null data to an empty string.
This is being caused by trying to create a NSString object with a NULL string. It is on one of these lines:
[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, ...)];
So, before you create a NSString with the results of the sql statement you need to check for NULL like this:
char *tmp = sqlite3_column_text(statement, 1);
if (tmp == NULL)
postId = nil;
else
postId = [[NSString alloc] initWithUTF8String:tmp];
Reference: lnafziger
Im working on a iOS app, so i need to save a image that the user will choose from his album or will take it from the camera. Im doing this with a picker
Then i try to save the image into the table with this code:
- (IBAction)saveUOme:(id)sender
{
NSString * _name = name.text;
NSString * _lastname = lastname.text;
NSString * _email = email.text;
NSString * _workemail = workEmail.text;
NSString * _cellphone = cell.text;
NSString * _phone = homePhone.text;
NSString * _money = amount.text;
NSString * _stuff = stuff.text;
NSString * _cause = cause.text;
//NSData *_image = UIImagePNGRepresentation(image);
NSData *_image = [NSData dataWithData:UIImagePNGRepresentation(image)];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"yyyy-MM-dd"];
NSString * _date = [dateformatter stringFromDate:[datePicker date]];
if (!switchR.on)
{
_date=#"";
}
//NSString *_error;
NSLog(#" %# ,%# ,%# ,%# ,%# ,%#,%#,%#,%#,%# ", _name,_lastname,_email,_workemail,_cellphone,_phone,_money,_stuff,_cause,_date);
if(([_name length]!=0 && [_lastname length] !=0 && [_money length] !=0) || ([_name length]!=0 && [_lastname length] !=0 && [_stuff length] !=0 ) )
{
NSString *query = [NSString stringWithFormat:#"INSERT INTO addUOmeTable ('name','lastname','email','workemail','cellphone','phone','money','stuff','cause','date','image') VALUES ('%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#');", _name, _lastname, _email, _workemail, _cellphone, _phone,_money,_stuff,_cause,_date,_image];
char *err;
if (sqlite3_exec(_db, [query UTF8String], NULL, NULL, &err) != SQLITE_OK)
{
sqlite3_close(_db);
NSAssert(0,#"No se pudo agregar");
}else
{
NSLog(#"Se agregaron los datos");
}
}else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Some Data is Missing"
message:#"Name, Lastname or Amount/Stuff"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
/*
FirstViewController *obj =[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
[self.view addSubview:obj.view];*/
//[self.navigationController pushViewController:obj animated:YES];
}
}
So far i think this works because in the database i get this as save object
<89504e47 0d0a1a0a 0000000d 49484452 00000400 00000300 08020000 0035d882 5a000000 1c69444f 54000000 02000000 00000001 80000000 28000001 80000001 80000c6c fd9ea091 34000040 00494441 5478019c 7df7775c 45b6357f ce7b3360 a5ce4959 b2255956 ce9d734e ea56ce96 73c0186c c0806d8c 73ced9c6 e4641be7 086f6698 21e300cc fbf9dbb7 4feb50be 2d9b79df 5a67d5da 75ead4a9 bab7c33d fb567a2e 38520e09 0f574222 4395e1c1 8ae84065 46aac37d 95c1de4a 7fa2dc93 2873c74b 9db1325b acbc275c da1d2eef 8c967644 4ada22a6 d6b0be29 ac6b8ea8 5b23eaf6 88aa3ba6 b624d4ce 84d693d2 85fb0df1 2153efa0 2135a84b 0de953a.....>
Then i try to select all from the database with this code
-(void) selectData
{
NSString *query = [NSString stringWithFormat:#"SELECT * FROM addUOmeTable ORDER BY name ASC"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_db, [query UTF8String], -1, &statement, Nil)== SQLITE_OK)
{
while (sqlite3_step(statement)==SQLITE_ROW)
{
char *id1 = (char *) sqlite3_column_text(statement, 0);
NSString *id_uomeDoubt = [[NSString alloc] initWithUTF8String:id1];
char *name1 = (char *) sqlite3_column_text(statement, 1);
NSString *name = [[NSString alloc] initWithUTF8String:name1];
char *lastname1 = (char *) sqlite3_column_text(statement, 2);
NSString *lastname = [[NSString alloc] initWithUTF8String:lastname1];
char *email1 = (char *) sqlite3_column_text(statement, 3);
NSString *email = [[NSString alloc] initWithUTF8String:email1];
char *workemail1 = (char *) sqlite3_column_text(statement, 4);
NSString *workemail = [[NSString alloc] initWithUTF8String:workemail1];
char *cellphone1 = (char *) sqlite3_column_text(statement, 5);
NSString *cellphone = [[NSString alloc] initWithUTF8String:cellphone1];
char *phone1 = (char *) sqlite3_column_text(statement, 6);
NSString *phone = [[NSString alloc] initWithUTF8String:phone1];
char *money1 = (char *) sqlite3_column_text(statement, 7);
NSString *money = [[NSString alloc] initWithUTF8String:money1];
char *staff1 = (char *) sqlite3_column_text(statement, 8);
NSString *staff = [[NSString alloc] initWithUTF8String:staff1];
char *couse1 = (char *) sqlite3_column_text(statement, 9);
NSString *couse = [[NSString alloc] initWithUTF8String:couse1];
char *date1 = (char *) sqlite3_column_text(statement, 10);
NSString *date = [[NSString alloc] initWithUTF8String:date1];
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 11) length:sqlite3_column_bytes(statement, 11)];
doubtArr = [NSArray arrayWithObjects:id_uomeDoubt,name,lastname,email,workemail,cellphone,phone,money,staff,couse,date,data, nil];
[uomeDoubts addObject:doubtArr];
}
}
[self.myTable reloadData];
}
And finally i try to display the image into a cell using this code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell)
{
cell= [[CustomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSArray *debtor = [uomeDoubts objectAtIndex:indexPath.row];
NSString *nombre = [debtor objectAtIndex:1];
NSString *nombre2 = [debtor objectAtIndex:1];
NSString *apellido = [debtor objectAtIndex:2];
nombre = [nombre stringByAppendingString:#" "];
nombre = [nombre stringByAppendingString:[debtor objectAtIndex:2]];
cell.TitleLable.text = nombre;
NSString *tempo = [debtor objectAtIndex:7] ;
if ([tempo length] ==0)
{
NSString *st = #"Stuff - ";
st = [st stringByAppendingString:[debtor objectAtIndex:8]];
cell.SubtitleLable.text = st;
}
else
{
NSString *mny = #"Money - $";
mny = [mny stringByAppendingString:[debtor objectAtIndex:7]];
cell.SubtitleLable.text = mny;
}
NSLog(#"%# %#",nombre2,apellido);
NSData* data = [debtor objectAtIndex:11];
cell.imageView.image = [UIImage imageWithData:data];
return cell;
}
And i dont get any error , the only thing is that the image is not display at all
PLEASE HELP!
I might be doing this all wrong as I am still new to Objective C.
I am trying to retrieve data from an sqlite3 database and store it in a NSObject array.
Here is the viewDidLoad method in my implementation file.
- (void)viewDidLoad
{
[super viewDidLoad];
clients = [[NSMutableArray alloc] init];
[self openDB];
NSString *sql = [NSString stringWithFormat:#"SELECT * FROM clients"];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
while (sqlite3_step(statement)==SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
char *field2 = (char *) sqlite3_column_text(statement, 1);
NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];
char *field3 = (char *) sqlite3_column_text(statement, 2);
NSString *field3Str = [[NSString alloc]initWithUTF8String:field3];
char *field4 = (char *) sqlite3_column_text(statement, 3);
NSString *field4Str = [[NSString alloc]initWithUTF8String:field4];
char *field5 = (char *) sqlite3_column_text(statement, 4);
NSString *field5Str = [[NSString alloc]initWithUTF8String:field5];
char *field6 = (char *) sqlite3_column_text(statement, 5);
NSString *field6Str = [[NSString alloc]initWithUTF8String:field6];
char *field7 = (char *) sqlite3_column_text(statement, 6);
NSString *field7Str = [[NSString alloc]initWithUTF8String:field7];
char *field8 = (char *) sqlite3_column_text(statement, 7);
NSString *field8Str = [[NSString alloc]initWithUTF8String:field8];
char *field9 = (char *) sqlite3_column_text(statement, 8);
NSString *field9Str = [[NSString alloc]initWithUTF8String:field9];
ClientObj *c1 = [[ClientObj alloc] initWithCompanyName:field1Str firstName:field2Str lastName:field3Str email:field4Str workPhone:field5Str mobilePhone:field6Str streetAddress:field7Str city:field8Str postalCode:field9Str];
}
}
self.clients = [NSArray arrayWithObjects:c1,c2,c3,c4, nil];
}
I am trying to increment *c1 each time the while statement loops through the database. eg. c1, c2 ,c3, c4
I am then trying to add each ClientObj created from the database loop above to the clients array but I am not sure how to do this.
Any help or advice is very much appreciated
Initialize the array once before the loop:
self.clients = [[NSMutableArray alloc] init];
and then just add each client object to the array inside the loop:
while (sqlite3_step(statement) == SQLITE_ROW) {
// ...
ClientObj *c = [[ClientObj alloc] initWithCompanyName:field1Str firstName:field2Str lastName:field3Str email:field4Str workPhone:field5Str mobilePhone:field6Str streetAddress:field7Str city:field8Str postalCode:field9Str];
[self.clients addObject:c];
}