0

我在我的 iPhone 应用程序中使用 SQLite3 从表 (tbresults) 中选择数据,对数据进行一些计算并将其显示在我的 uitableview 中。sql 命令使用 SUM 函数,我的应用程序似乎不喜欢它。

我从表中进行选择的方法如下,但处理似乎在下一行失败。

if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)

该方法的完整代码如下。

+ (void) getLeagueTable {

Tag_TrackerAppDelegate *appDelegate = (Tag_TrackerAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.tbresultsArray = [[NSMutableArray alloc] init];


const char *sql = "select a.resultid, a.teamname, sum(b.played), sum(b.win), " 
"sum(b.draw), sum(b.lose), sum(b.for), sum(b.against), sum(b.win * 3 + b.draw) "
"from tbteam a, tbresults b, tbseason c where a.teamid = b.teamid and c.active = 'Y'" 
"and b.seasonid = c.seasonid group by b.teamid order by points desc;";


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

    sqlite3_stmt *selectstmt;

    while(sqlite3_step(selectstmt) == SQLITE_ROW) {

        NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
        tbresults *resultsObj = [[tbresults alloc] initWithPrimaryKey:primaryKey];

        resultsObj.teamname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];

        resultsObj.played = sqlite3_column_int(selectstmt, 2);
        resultsObj.won = sqlite3_column_int(selectstmt, 3);
        resultsObj.drawn = sqlite3_column_int(selectstmt, 4);
        resultsObj.lost = sqlite3_column_int(selectstmt, 5);
        resultsObj.For = sqlite3_column_int(selectstmt, 6);
        resultsObj.Against = sqlite3_column_int(selectstmt, 7);
        resultsObj.points = sqlite3_column_int(selectstmt, 8);

        [appDelegate.tbresultsArray addObject:resultsObj];
        [resultsObj release];
    }
}
else
    sqlite3_close(database); 

}

有谁知道在 iPhone 上使用 SUM 功能是否有问题?我已经检查并仔细检查了其余代码。数据库正在成功打开。

任何帮助,将不胜感激。

4

2 回答 2

0

你得到什么样的错误?

我认为您的选择语句无效,停止选择列 a.resultid 或将此列包含在 group by 中,它将起作用。

于 2009-08-03T18:48:32.407 回答
0

您至少需要一个适当的 group by 子句:

  "select a.resultid, a.teamname, sum(b.played), sum(b.win), " 
"sum(b.draw), sum(b.lose), sum(b.for), sum(b.against), sum(b.win * 3 + b.draw) "
"from tbteam a, tbresults b, tbseason c where a.teamid = b.teamid and c.active = 'Y'" 
"and b.seasonid = c.seasonid group by a.resultid, a.teamname order by points desc;";
于 2009-08-03T18:56:34.700 回答