【问题标题】:How to fetch All data of the columns in the table如何获取表中列的所有数据
【发布时间】:2013-05-04 07:01:33
【问题描述】:

这是我的代码... 我想获取所有具有 320 个报价的报价数据,但它只获取第一个报价。请帮帮我

-(NSMutableArray *)getAllQuotesData
{
    NSMutableArray *quotesArray = [[NSMutableArray alloc] init];

    NSString *sqlStr = [NSString stringWithFormat:@"SELECT quote FROM quotes"];

    sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement:sqlStr];

    while (sqlite3_step(ReturnStatement)==SQLITE_ROW)
    {
        @try
        {
            QuotesDC *myQuote = [[QuotesDC alloc] init];

             NSString *user_id = [NSString stringWithUTF8String:(char 
*)sqlite3_column_text(ReturnStatement,0)];

             NSString *category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,1)];

             NSString *subcategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,2)];

             NSString *quote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,0)];

             NSString *star = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,4)];

            myQuote.userid = [user_id integerValue];
            myQuote.category = category;
            myQuote.subcategory = subcategory;
            myQuote.quote = quote;
            myQuote.star = star;

            [quotesArray addObject:myQuote];
            NSLog(@"%u", quotesArray.count);
        }
        @catch (NSException *ept) {
            NSLog(@"Exception in %s, Reason: %@", __PRETTY_FUNCTION__, [ept reason]);
        }

        return  quotesArray;
    }
}

【问题讨论】:

    标签: iphone ios objective-c xcode sqlite


    【解决方案1】:

    您的代码没有问题只需做一件事return dataArray 将在循环之外,您将加载所有这样的引号,接受我的回答谢谢

    -(NSMutableArray *)getAllQuotesData
    {
        NSMutableArray *quotesArray = [[NSMutableArray alloc] init];
    
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT quote FROM quotes"];
    
        sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement:sqlStr];
    
        while (sqlite3_step(ReturnStatement)==SQLITE_ROW)
        {
            @try
            {
                QuotesDC *myQuote = [[QuotesDC alloc] init];
    
                 NSString *user_id = [NSString stringWithUTF8String:(char 
    *)sqlite3_column_text(ReturnStatement,0)];
    
                 NSString *category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,1)];
    
                 NSString *subcategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,2)];
    
                 NSString *quote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,0)];
    
                 NSString *star = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement,4)];
    
                myQuote.userid = [user_id integerValue];
                myQuote.category = category;
                myQuote.subcategory = subcategory;
                myQuote.quote = quote;
                myQuote.star = star;
    
                [quotesArray addObject:myQuote];
                NSLog(@"%u", quotesArray.count);
            }
            @catch (NSException *ept) {
                NSLog(@"Exception in %s, Reason: %@", __PRETTY_FUNCTION__, [ept reason]);
            }
    
    
        }
    return  quotesArray;
    }
    

    【讨论】:

    • 谢谢先生,这是 100% 正确的 .. 谢谢你解决了我的问题
    【解决方案2】:

    用这个替换你的查询字符串

     NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM quotes WHERE quote=320"];
    

    【讨论】:

      【解决方案3】:

      使用此代码,这可能会对您有所帮助

      -(void)retrivequoteFromDB
      {
          NSMutableArray * quoteArray=[[NSMutableArray alloc]init];
      
          sqlite3 *sqliteDB;
      
          sqlite3_stmt *compiledStatement = NULL;
      
          if(sqlite3_open([databasePath UTF8String], &sqliteDB)==SQLITE_OK)
          {
              NSString *sql=@"SELECT quote FROM quotes";
      
              const char *sqlC=[sql UTF8String];
      
              if(sqlite3_prepare(sqliteDB, sqlC, -1, &compiledStatement, NULL)==SQLITE_OK)
              {
                  while(sqlite3_step(compiledStatement) == SQLITE_ROW)
                  {
                      NSString *quote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
      
                      [quoteArray addObject:quote];
                  }
              }
              sqlite3_reset(compiledStatement);
          }
          sqlite3_finalize(compiledStatement);
      }
      

      【讨论】:

        【解决方案4】:

        尝试像这样检查您的数据库表 320 记录是否存在,然后它会提供所有数据。

        SELECT quote FROM quotes  //it'l gives only quote columnvalues from quotes table.
        

        如果您想获取所有数据,请使用星号“*”符号代替引号。

        否则尝试这样,

         SELECT quote FROM quotes limit 320 //it'l gives top 320 records
        

        一旦阅读了这篇文章,您就会发现问题所在。 select Query

        【讨论】:

        • @user1939575: 如果你的数据库有 320 条记录,它会给出正确的结果。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 2022-01-20
        • 2021-12-05
        • 1970-01-01
        • 2019-07-30
        • 2016-06-12
        • 2011-09-27
        相关资源
        最近更新 更多