Sqlite 数据检索问题

发布于 2024-11-04 20:46:00 字数 1686 浏览 0 评论 0原文

我有一个 SQLite 数据库,当我尝试从数据库获取数据时,我会重复获取最后插入的元素。我怎样才能获得所有元素而不重复。

我写的代码:

- (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;
    }

我正在检索数据如下:

NSMutableArray *sqlArray = [sqlViewController gettingData]; 

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2024-11-11 20:46:00

只需全局声明您的数组,而不是在您的方法中本地声明。你的问题将会得到解决。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文