简体   繁体   中英

Problem With Sqlite Data Retrieving

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM