SQLite分析游标数据
我正在编写一个测验并将各个答案记录在 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 cursorCursor 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的原始查询如下所示:
您将使用 Group By 将所有行与将同一会话放在一起,然后使用求和和计数函数来计算会话内的细分。请注意,此处的“求和”仅有效,因为正确答案为 1,错误答案为 0,如果更改记录答案的方式,则必须稍微更改查询。
我将尝试猜测 db.query 调用的外观:
我将 String 数组声明分开以将其清理一下,但我认为其功能应该相同。
A simple raw query would be like this:
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:
I separated the String array declarations to clean it up a bit but I think that should function the same.
您正在使用 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.