Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:42:20+00:00 2026-06-10T21:42:20+00:00

Can anyone please help me out.I have been stuck with this NSMutableArray for last

  • 0

Can anyone please help me out.I have been stuck with this NSMutableArray for last 2-3 days. I am beginner in Objective-C and I don’t know what is the error in the code.In my program, I have created a sqlite database.It contain 3 fields and i have successfully inserted the values into db.And I am extracting these values from the db into NSMutableArray and displaying this array.But the problem is only last element is displaying and not whole data. I dont understand why it is displaying only the last element.I will paste the code below- Can anyone please check it and help me out.

- (void)viewDidLoad
{

   NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDir = [documentPaths objectAtIndex:0];

   databasePath = [documentsDir stringByAppendingPathComponent:@"TDL1.db"];

   const char *dbpath = [databasePath UTF8String];

   sqlite3_stmt    *statement;

        if (sqlite3_open(dbpath, &TDLDB) == SQLITE_OK)
         {
                    NSString *querySQL = [NSString stringWithFormat: @"SELECT MYTASK, SUBTASK FROM TDL1 "];

                    const char *query_stmt = [querySQL UTF8String];



      if (sqlite3_prepare_v2(TDLDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)

       {
            while (sqlite3_step(statement) == SQLITE_ROW)
              {
                     todolist2=[[NSMutableArray alloc]init];

                      NSString *mytask = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];

                      NSString *subtask = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];

                   todolist2 = [NSMutableArray arrayWithObjects:mytask,subtask, nil];

                       NSLog(@"THE array is:%@",todolist2);

       } 

                sqlite3_finalize(statement);
}
              sqlite3_close(TDLDB);

}
         NSLog(@"THE outside array is:%@",todolist2);
[super viewDidLoad];

}

And in the console I am getting the following output :-

2012-09-06 11:33:23.529 TDL[823:f803] THE array is:(
    task0,
    task0
)
2012-09-06 11:33:23.532 TDL[823:f803] THE array is:(
    task1,
    task1
)
2012-09-06 11:33:23.533 TDL[823:f803] THE array is:(
    task2,
    task2
)
2012-09-06 11:33:23.534 TDL[823:f803] THE array is:(
    task3,
    task3
)
2012-09-06 11:33:23.534 TDL[823:f803] THE array is:(
    task4,
    task4
)
2012-09-06 11:33:23.535 TDL[823:f803] THE array is:(
    task5,
    task5
)
2012-09-06 11:33:23.536 TDL[823:f803] THE array is:(
   task6,
   task6
)
2012-09-06 11:33:23.537 TDL[823:f803] THE array is:(
    task6,
    task6
)
2012-09-06 11:33:23.537 TDL[823:f803] THE array is:(
    task7,
    task7
)
2012-09-06 11:33:23.538 TDL[823:f803] THE array is:(
    task7,
    task7
)
2012-09-06 11:33:23.539 TDL[823:f803] THE array is:(
    task8,
    task8
)
2012-09-06 11:33:23.539 TDL[823:f803] THE array is:(
    task9,
    task9
)
2012-09-06 11:33:23.540 TDL[823:f803] THE array is:(
   task10,
   task10
)
2012-09-06 11:33:23.540 TDL[823:f803] THE outside array is:(
   task10,
   task10
)
2012-09-06 11:33:23.541 TDL[823:f803] could not prepare statement: not an error

I want to display the values starting from task0 to task10.How to show that?Please tell me the solution.Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:42:21+00:00Added an answer on June 10, 2026 at 9:42 pm

    You are just making a new NSMutableArray each time you go through your while (sqlite3_step(statement) == SQLITE_ROW) loop. Actually you are making one twice. Move the following line outside the while loop:

    todolist2=[[NSMutableArray alloc]init];
    

    Then inside your while loop instead of recreating a new array each time with:

    todolist2 = [NSMutableArray arrayWithObjects:mytask,subtask, nil];
    

    Instead use:

    [todolist2 addObject:myTask];
    [todolist addObject:subTask];
    

    These lines will add the two strings to the end of your array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please can anyone help me out with this problem? This code below works perfectly
Can anyone please help me to work out how to achieve the following? I
Can anyone help me out please? I'm confused. I want to set up my
Can anyone please help me with this...I'm not very good with regular expressions and
Can anyone please help. I'm following a tutorial found here as I have a
Can anyone please help me how to convert string to date I have 3
Can anyone please help in finding the solution of the above question I have
Can anyone please help me to know how to open an excel sheet in
Can anyone please help me to add video controls (play, pause, forward, seekbar) to
Can anyone please help me to understand how should i access json data in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.