SQLite分析游标数据

发布于 2024-11-02 23:09:40 字数 762 浏览 1 评论 0原文

我正在编写一个测验并将各个答案记录在 SQLite 数据库中。我的表格具有以下格式:

ID、会话、RightWrong
1, 042102111244, 0
2, 042102111244, 1
3, 042102111244, 1
4, 042102111244, 1
5, 042102111244, 0
6, 042102111412, 0
7, 042102111412, 1
8, 042102111412, 0
9, 042102111412, 1

其中“会话”指的是某个测验开始的时间,每行代表一个答案(0=错误答案,1=正确)。

我只关心“总数”,即每个测验正确回答了多少个问题(以及多少个问题中)。

所以我的“梦想”光标将是:

Session、RightAnswers、TotalNoOfQuestions
042102111244, 3, 5
042102111412, 2, 4

有办法实现吗?我无法找出合适的查询(诚然,这与其说是编程问题,不如说是逻辑问题)。

目前,我只是将所有条目读取到游标中
光标游标 = db.query(MyDataSQLHelper.TABLE, null, null, null, null, 空,空); 并认为我宁愿做一些“后处理”,但不确定什么是最好的方法。

感谢您的帮助,
缺口

I'm coding a quiz and record individual answers in a SQLite-Database. My table has the following format:

ID, Session, RightWrong
1, 042102111244, 0
2, 042102111244, 1
3, 042102111244, 1
4, 042102111244, 1
5, 042102111244, 0
6, 042102111412, 0
7, 042102111412, 1
8, 042102111412, 0
9, 042102111412, 1

Where "session" refers to the time a certain quiz was started, and each row represents a single answer (0=Wrong answer, 1=Correct).

I only care about the "aggregate", i.e. how many questions were answered correctly (& out of how many questions) per quiz.

So my "dream" cursor would be:

Session, RightAnswers, TotalNoOfQuestions
042102111244, 3, 5
042102111412, 2, 4

Is there a way to achieve this? I'm having trouble figuring out a fitting query (which is admittedly less of a programming-problem than a logic-issue).

At the moment, I'm just reading all entries into a cursor
Cursor cursor = db.query(MyDataSQLHelper.TABLE, null, null, null, null,
null, null);

and thought that I would rather do some "post-processing", but am unsure what's the best approach.

Thanks for your help,
Nick

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

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

发布评论

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

评论(2

鹿童谣 2024-11-09 23:09:40

一个简单的原始查询如下所示:

Cursor cursor = db.rawQuery("select session, sum(rightwrong) as RightAnswers, count(rightWrong) as TotalNoOfQuestions from tablename group by session");

您将使用 Group By 将所有行与将同一会话放在一起,然后使用求和和计数函数来计算会话内的细分。请注意,此处的“求和”仅有效,因为正确答案为 1,错误答案为 0,如果更改记录答案的方式,则必须稍微更改查询。

我将尝试猜测 db.query 调用的外观:

String[] columns = {"Session", "sum(RightWrong) as RightAnswers", "count(RightWrong) as TotalNoOfQuestions";
String[] groupBy = {"Session"};


Cursor cursor = db.query("tablename", columns, null, null, groupBy, null, null);

我将 String 数组声明分开以将其清理一下,但我认为其功能应该相同。

A simple raw query would be like this:

Cursor cursor = db.rawQuery("select session, sum(rightwrong) as RightAnswers, count(rightWrong) as TotalNoOfQuestions from tablename group by session");

You will use Group By to combine all rows with the same session together, then use the sum and count functions to figure out the breakdown within session. Note "sum" here only works because right answers are 1 and wrong answers are 0, if you change the way answers are recorded you will have to change the query a bit.

I'll try to guess how the db.query call would look:

String[] columns = {"Session", "sum(RightWrong) as RightAnswers", "count(RightWrong) as TotalNoOfQuestions";
String[] groupBy = {"Session"};


Cursor cursor = db.query("tablename", columns, null, null, groupBy, null, null);

I separated the String array declarations to clean it up a bit but I think that should function the same.

も让我眼熟你 2024-11-09 23:09:40

您正在使用 group by 子句。

您正在使用
公共游标查询(字符串表,字符串[]列,字符串选择,
String[] SelectionArgs、String groupBy、Stringhaving、
字符串 orderBy)

例如。
游标cursor = db.query(MyDataSQLHelper.TABLE,
new String[] {"Session", "sum(RightWrong) RightAnswers", "count(Session) TotalNoOfQuestions"},
无效的,
“会议”,
无效的,
无效的);

你再试一次。

You are using group by clause.

You are using
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy)

ex.
Cursor cursor = db.query(MyDataSQLHelper.TABLE,
new String[] {"Session", "sum(RightWrong) RightAnswers", "count(Session) TotalNoOfQuestions"},
null,
"Session",
null,
null);

You try again.

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