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 7992405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:33:13+00:00 2026-06-04T13:33:13+00:00

I have a sqlite database with questions and I want to write a function

  • 0

I have a sqlite database with questions and I want to write a function to get N random questions from it. I use a for loop with n iterations. For each iteration I get a question with a random ID which is not repeated. I copy the element of each column in a variable of an object of the class “question”. Until here, everything is perfect. I have used NSLog to print the questions through all the function and all the process is right until I try to add the last object to a NSMutablearray. All the positions are overwritten.
This is my code:

-(NSMutableArray*)getNQuestions:(int)n
{
    question *aQuestion=[[question alloc]init];

    NSMutableArray *arrayOfChoosenQuestions=[[NSMutableArray alloc]initWithCapacity:n];

    NSMutableIndexSet *choosenQuestionsIDs=[[NSMutableIndexSet alloc]init];

    FMResultSet *rs;
    int questionID;

    for (int i=0; i<n; i++) {
       questionID = (arc4random()%(NUMBER_OF_AVAILABLE_QUESTIONS-1))+1;
        while ([choosenQuestionsIDs containsIndex:questionID]) {
            questionID = (arc4random()%(NUMBER_OF_AVAILABLE_QUESTIONS-1))+1;
        }
        [choosenQuestionsIDs addIndex:questionID];

        rs = [database executeQueryWithFormat:@"SELECT * FROM questions WHERE Questions.ID=%d", questionID];

        if ([rs next] ) {
            aQuestion.statement=[rs stringForColumn:@"Question"];
            aQuestion.rightAnswer=[rs stringForColumn:@"Right_Answer"];
            aQuestion.wrongAnswer1=[rs stringForColumn:@"Wrong_Answer_1"];
            aQuestion.wrongAnswer2=[rs stringForColumn:@"Wrong_Answer_2"];
            aQuestion.image=[rs stringForColumn:@"image"];
        }
        [arrayOfChoosenQuestions addObject:aQuestion];
    }
    return arrayOfChoosenQuestions;
}

This is an example of what happen

1rst iteration:

arrayOfChoosenQuestions=[question1]

2nd iteration

arrayOfChoosenQuestions=[question2 question2]

3rd iteration

arrayOfChoosenQuestions=[question3 question3 question3]

Thank You for your help

  • 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-04T13:33:14+00:00Added an answer on June 4, 2026 at 1:33 pm

    You are re-using the same question object (which is aQuestion) over and over again, so you are actually modifying and inserting the same underlying object into the array.

    Basically, in your current code, you are referring to the same block of memory when you are reading from/writing to aQuestion. All modification will go to the same position in memory. As you can see, the change “propagate” to other objects, since they are actually the same memory block referred to by the pointer in aQuestion.

    For each loop, you have to create a new question object to accommodate new data.

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

Sidebar

Related Questions

I have a sqlite database and I want to connect from my C# program
I have one Sqlite Database file. I want to share my sqlite database between
I have an SQLite database file exported from Scraperwiki with .sqlite file extension. How
When you want to write a query in Python that will select (from SQLite
I have a SQLite database in my Android application, and I want to update
I have a sqlite database in my application. I want to make an expandable
I have 4 tables in my SQLite database. One with questions, one with answers
I have a SQLite database, and what i want to do is that an
I have a SQLite database that contains a huge set of log messages. I
I have an sqlite database, and I would like to keep it read-only without

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.