我的ResultSet只有使用.next()时返回一行,但我确实有3行。

huangapple go评论80阅读模式
英文:

My ResultSet is only doing one row with .next(), but I do have 3

问题

这是我的代码:

ResultSet chaserResults = LiteSQL.onQuery("SELECT count(*), id, name, geburt, geschlecht, speed, speedf, strength, strengthf, " +
                    "val3, val3f, val4, val4f, val5, val5f, position FROM player WHERE position = 'Chaser' AND teamid = " + teamid);
setchaser = new Chaser[chaserResults.getInt("count(*)")];
int i = 0;
while(chaserResults.next()){
    setchaser[i] = new Chaser(chaserResults.getInt("id"), chaserResults.getString("name"), chaserResults.getInt("geburt"),
            teamResults.getInt("id"), chaserResults.getInt("geschlecht"), chaserResults.getInt("speed"),
            chaserResults.getInt("speedf"), chaserResults.getInt("strength"), chaserResults.getInt("strengthf"),
            chaserResults.getInt("val3"), chaserResults.getInt("val3f"), chaserResults.getInt("val4"),
            chaserResults.getInt("val4f"), chaserResults.getInt("val5"), chaserResults.getInt("val5f"),
            chaserResults.getString("position"));
    i++;
    System.out.println(i);
}

对于你的代码,我已经将HTML转义字符(例如")替换为正常的双引号,并删除了不需要的翻译部分。

英文:

Here is my code:

ResultSet chaserResults = LiteSQL.onQuery("SELECT count(*), id, name, geburt, geschlecht, speed, speedf, strength, strengthf, " +
                "val3, val3f, val4, val4f, val5, val5f, position FROM player WHERE position = 'Chaser' AND teamid = " + teamid);
        setchaser = new Chaser[chaserResults.getInt("count(*)")];
        int i = 0;
        while(chaserResults.next()){
            setchaser[i] = new Chaser(chaserResults.getInt("id"), chaserResults.getString("name"), chaserResults.getInt("geburt"),
                    teamResults.getInt("id"), chaserResults.getInt("geschlecht"), chaserResults.getInt("speed"),
                    chaserResults.getInt("speedf"), chaserResults.getInt("strength"), chaserResults.getInt("strengthf"),
                    chaserResults.getInt("val3"), chaserResults.getInt("val3f"), chaserResults.getInt("val4"),
                    chaserResults.getInt("val4f"), chaserResults.getInt("val5"), chaserResults.getInt("val5f"),
                    chaserResults.getString("position"));
            i++;
            System.out.println(i);
        }

I'm sorry for my bad coding!I don't really know why, but it stops after the first row. I think it could be the line "chaserResult.getInt("count(*)") thing, but I don't know how to replace it, because I need a method that counts the rows.

答案1

得分: 2

"count(*)" 在结果中表示查询只返回1行。

由于 "count()" 是一个 聚合函数,所以这个查询是一个 聚合查询,正如SQLite文档所述:

如果SELECT语句是一个没有GROUP BY子句的聚合查询,那么结果集中的每个聚合表达式在整个数据集上都会被计算一次。结果集中的每个非聚合表达式对于数据集中的任意选择的一行都会被计算一次。同一个任意选择的行对于每个非聚合表达式都是一样的。或者,如果数据集中没有行,则每个非聚合表达式都会针对一个由NULL值完全组成的行进行计算。

通过计算结果集中的聚合和非聚合表达式创建的单行结果集数据形成了没有GROUP BY子句的聚合查询的结果。没有GROUP BY子句的聚合查询始终返回一行数据,即使没有输入数据也是如此。

英文:

Having count(*) in the result means that the query only returns 1 row.

Since count() is an aggregate function, the query is an aggregate query, and as the SQLite Documentation says:

> If the SELECT statement is an aggregate query without a GROUP BY clause, then each aggregate expression in the result-set is evaluated once across the entire dataset. Each non-aggregate expression in the result-set is evaluated once for an arbitrarily selected row of the dataset. The same arbitrarily selected row is used for each non-aggregate expression. Or, if the dataset contains zero rows, then each non-aggregate expression is evaluated against a row consisting entirely of NULL values.
>
> The single row of result-set data created by evaluating the aggregate and non-aggregate expressions in the result-set forms the result of an aggregate query without a GROUP BY clause. An aggregate query without a GROUP BY clause always returns exactly one row of data, even if there are zero rows of input data.

huangapple
  • 本文由 发表于 2020年8月13日 08:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63386246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定