访问游标时出现异常
我的应用程序让用户将记录输入数据库。这些记录包含自动递增的 ID、日期(以 mm-dd-yyyy 格式的文本)、数量 (int) 和一些其他字段。
我正在尝试获取所有月份以及每个月的总数量。这样我就可以在我的应用程序中显示它们,例如:Dec 2010 - 90qty,Jan 2011 - 45qty...
我正在使用此查询来执行此操作:
return mDb.query("record", new String[] {KEY_ROWID, "strftime('%m-%Y', date)", "sum(quantity)"}, null, null, "strftime('%m-%Y', date)", null, null);
我相信查询很好,但在访问光标时出现异常。它说“日期”不是一列。如果我将 from[] 中的“date”重命名为“dateA”,则会出现异常,“dateA”不是列,因此我相信这会缩小问题范围。
Cursor c = mDbHelper.fetchReport();
String[] from = new String[] { "date", "quantity" };
int[] to = new int[] { R.id.text1, R.id.text2 };
setListAdapter(new SimpleCursorAdapter(this, R.layout.basicrow, c, from, to) {
}
有什么想法吗?
更新:执行 select strftime('%m-%Y', date), sum(quantity) from record 时;在 adb shell 中,它返回“:94”...数量存在(但对于所有日期)但日期不存在。
当我输入时,从记录中选择 strftime(date) ,它返回所有日期(在此测试用例中为 3)。当我输入时,从记录中选择 strftime('%m-%Y', date) ,它返回 3 个空白行。
My application has users entering records into a database. The records contain an auto-incrementing id, date (as text in mm-dd-yyyy format), a quantity (int) and a few other fields.
I'm trying to grab all the months and the sum quantity for each month. So that I can display them in my app like: Dec 2010 - 90qty, Jan 2011 - 45qty...
I'm using this query to do that:
return mDb.query("record", new String[] {KEY_ROWID, "strftime('%m-%Y', date)", "sum(quantity)"}, null, null, "strftime('%m-%Y', date)", null, null);
I believe the query is good but I get an exception when accessing the cursor. It says 'date' is not a column. If I rename "date" in from[] to "dateA", the exception reads, 'dateA' is not a column, so I believe that narrows the issue.
Cursor c = mDbHelper.fetchReport();
String[] from = new String[] { "date", "quantity" };
int[] to = new int[] { R.id.text1, R.id.text2 };
setListAdapter(new SimpleCursorAdapter(this, R.layout.basicrow, c, from, to) {
}
Any ideas?
Update: When performing select strftime('%m-%Y', date), sum(quantity) from record; in adb shell, it returns ":94" ... the quantity is there (for all dates however) but date is not.
When I enter, select strftime(date) from record, it returns all the dates (3 in this test case). When I enter, select strftime('%m-%Y', date) from record, it returns 3 blank rows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信会发生以下情况。这是您的查询(简化):
您不选择日期列;您选择一个将列作为参数的函数。在这种情况下,数据库会为结果集列分配一些任意名称,例如
如您所见,结果集中不存在日期列(就像错误所述)。
您需要尝试的是:
or
并使用 'date' 或 'formatted_date' 来引用结果集列。
聚苯乙烯
这可能是不同的问题。如果插入的日期存储为文本,则 strftime() 函数将不知道如何格式化它。
I believe the following happens. Here is your query (simplified):
You do not select date column; you select a function which takes the column as a parameter. Databases in such cases assign the resultset columns some arbitrary names, such as
As you see, date column doesn't exist in the result set (just like the error says).
What you need to try is:
or
and use 'date' or 'formatted_date' to reference the resultset column.
P.S.
This might be the different issue. If date indded is stored as text, then strftime() function wouldn't know how to format it.