我已经创建和提交数据的数据库。有一栏在那里我想找到的最大价值。
I have a database created and filed with data. There is a column where i want to find the biggest value.
这是我的数据库适配器使用的方法:
This is the method used in my database adapter :
public Cursor getBiggestInTheColumn() {
return db.query(DATABASE_TABLE, null,
"MAX(price)", null, null, null, null);
}
这应该工作,但是当我调用方法:
It should work but when i call the method :
cursor = dbadp.getBiggestInTheColumn();
我得到一个运行时错误是这样的(LogCat中):
I Get a Runtime Error Like This(LogCat) :
12月七号日至14日:38:51.852:ERROR / AndroidRuntime(276):android.database.sqlite.SQLiteException:滥用聚合函数MAX():在编制:由引起SELECT * FROM spendings其中max(价格)
07-14 12:38:51.852: ERROR/AndroidRuntime(276): Caused by: android.database.sqlite.SQLiteException: misuse of aggregate function MAX(): , while compiling: SELECT * FROM spendings WHERE MAX(price)
任何想法?我怀疑这是由于糟糕的执行,但是这是我能拿出最好的。其他查询工作良好。
Any ideas? I suspect this is due to the bad query, but this is the best that i can come up with. Other queries are working well.
如果你只是想最大的价值,那么,你想要的SQL相当于:
SELECT MAX(价格)FROM spendings
。
If you just want the biggest value then you want the SQL equivalent of:
SELECT MAX(price) FROM spendings
.
我倾向于使用 db.rawQuery(SQL字符串,字符串[] selectionArgs两个)
,所以这将是: db.rawQuery(选择MAX(价格)FROM spendings,NULL)
。
I tend to use db.rawQuery(String sql, String[] selectionArgs)
, so it would be: db.rawQuery("SELECT MAX(price) FROM spendings", null)
.
不过,我想你可以用 db.query做到这一点(DATABASE_TABLE,新的String [] {MAX(价格)},NULL,NULL,NULL,NULL,NULL);
。虽然我没有测试过这一点。
But I think you could do this with db.query(DATABASE_TABLE, new String [] {"MAX(price)"}, null, null, null, null, null);
. Though I haven't tested that.