问题描述
- 获取表中列的全部数据
-
代码我想要获取所有引用数据,一共有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; }
}
解决方案
代码没有问题哦,只是返回的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;
}
时间: 2024-12-10 11:50:48