0

I have an SQLite database, and when I am trying to get the data from the database, I get the last inserted element repeatedly. How can I get all the elements with no repetition.

The code I've written:

- (NSMutableArray *) gettingData {


    sqlDict = [[NSMutableDictionary alloc] init];

    membersInfoArray =[[NSMutableArray alloc]init ];


     [self checkAndCreateDatabase];

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
     {

    const char *sql = "select * from ProductList";

     sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
    {
        while(sqlite3_step(selectstmt) == SQLITE_ROW)
        {


       NSString *prdbcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];

     [sqlDict setObject:prdbcode forKey:@"Barcode"];


     [prdbcode release];


      NSString *prdname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 


     [sqlDict setObject:prdname forKey:@"ProductName"];

      [prdname release];
       NSString *prdDesc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
      [sqlDict setObject:prdDesc forKey:@"ProductDescription"];
      [prdDesc release];
      NSString *prdstatus = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];

     [sqlDict setObject:prdstatus forKey:@"ProductStatus"];
     [prdstatus release];
     [membersInfoArray addObject:sqlDict];
     [sqlDict release];

        }

     }

     sqlite3_finalize(selectstmt);   

     }

     sqlite3_close(database);
     return membersInfoArray;
    }

I am retrieving the data as follows:

NSMutableArray *sqlArray = [sqlViewController gettingData]; 

Thank you.

7
  • @Prabah Nothing wrong in above method. How do you access the values ? Can you please post that code too ? Commented May 3, 2011 at 5:27
  • ok which code?? insertion of data na? Commented May 3, 2011 at 5:35
  • @Prabah You must be iterating returned array. That code. Commented May 3, 2011 at 5:36
  • NSMutableArray *sqlArray = [sqlViewController gettingData]; Commented May 3, 2011 at 5:38
  • @Prabah try cleaning the build from the application and deleting your application from simulator and re run. There is nothing wrong with the code above you are writing. And should not be the case, still open database from the installed location and verify the records. Commented May 3, 2011 at 5:49

1 Answer 1

1

Just Declare your array globally instead of declare locally in your method. Your problem will resolved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.