如何在android中的一个活动中进行两个数据库查询调用
我想进行一个 sql 查询并将其放入 TextView 中。我可以通过遵循有关使用适配器/dbHelpers 的教程/示例来为 ListView 执行此操作。我收到 CursorIndexOutOfBoundException
会让我相信我没有正确使用光标。
这是 onCreate 中的代码:
TextView summaryTV = (TextView) findViewById(R.id.summary);
sumAllAmounts = iouHelper.getSumAllAmounts();
startManagingCursor(sumAllAmounts);
System.out.println(String.valueOf(iouHelper.getSumAllAmounts(sumAllAmounts)));
summaryTV.setText(String.valueOf(iouHelper.getSumAllAmounts(sumAllAmounts)));
sumAllAmounts
是一个 Cursor,由数据库助手执行 sql 命令返回。我知道 sql 查询是正确的,因为我使用 SQLite 数据库浏览器对其进行了测试。我可以使用 ArrayAdapters 让它在 ListView 上工作。
查询应返回 2 列:id 和 amount。 helper.getSumAllAmounts(Cursor)
将返回第二列 Cursor.getInt(1)
并将其转换为要显示在 TextView 上的字符串。 TextView.setText(String.valueOf(int))
请指导我哪里搞砸了。谢谢。
编辑:
Code for DB helper function:
public Cursor getSumAllAmounts() {
return (getReadableDatabase().rawQuery("SELECT _id, SUM(amount) FROM ious", null));
}
public int getSumAllAmounts(Cursor c){
return (c.getInt(1));
}
数据库的架构:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE ious (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT, amount INT, description TEXT, date String);");
}
I would like to make a sql query and puts it in a TextView. I am able to do it for a ListView by following tutorials/examples on using adapters/dbHelpers. I am getting CursorIndexOutOfBoundException
will leads me to believe I am not using the Cursor properly.
This is the code in onCreate:
TextView summaryTV = (TextView) findViewById(R.id.summary);
sumAllAmounts = iouHelper.getSumAllAmounts();
startManagingCursor(sumAllAmounts);
System.out.println(String.valueOf(iouHelper.getSumAllAmounts(sumAllAmounts)));
summaryTV.setText(String.valueOf(iouHelper.getSumAllAmounts(sumAllAmounts)));
sumAllAmounts
is a Cursor which is returned by the database helper executing the sql command. I know the sql query is correct because I tested it using SQLite Database Browser
. And I am able to get it to work on a ListView
using ArrayAdapters
.
The query should return 2 column: id and amount. The helper.getSumAllAmounts(Cursor)
will return the second column Cursor.getInt(1)
and it gets converted to a String to be displayed on the TextView. TextView.setText(String.valueOf(int))
Please guide me as to where I messed up. Thanks.
EDIT:
Code for DB helper function:
public Cursor getSumAllAmounts() {
return (getReadableDatabase().rawQuery("SELECT _id, SUM(amount) FROM ious", null));
}
public int getSumAllAmounts(Cursor c){
return (c.getInt(1));
}
Schema for the DB:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE ious (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT, amount INT, description TEXT, date String);");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误位于
helper.getSumAllAmounts(Cursor)
中。您正在将无效的列索引传递给cursor.get* 函数。我需要查看您的查询投影字符串和对cursor.get* 的调用,以准确地告诉您出了什么问题。Your bug is in
helper.getSumAllAmounts(Cursor)
. You are passing an invalid column index into a cursor.get* function. I'd need to see your query projection string and your calls to cursor.get* to tell you exactly what's wrong.