我使用以下代码来获取过去两个月的数据。
public List<String> getDates() {
SQLiteDatabase db = this.getReadableDatabase();
List<String> dateList = new ArrayList<>();
String query = "SELECT DISTINCT " + KEY_PROGRESSDATE + " FROM " + TABLE_ENTRY + " WHERE progressdate >= date('now','start of month','-1 month') AND progressdate < date('now','start of month') desc";
Cursor c = db.rawQuery(query, null);
if (c.getCount() > 0) {
while (c.moveToNext()) {
dateList.add(c.getString(0));
}
}
return dateList;
}
但我得到了以下错误消息..
引起:android.database.sqlite.SQLiteException:靠近“desc”: 语法错误(代码1):,编译时:SELECT DISTINCT progressdate FROM条目WHERE progressdate&gt; = date('now','start of month',' - 1 月')和progressdate&lt; date('now','start of month')desc
为什么会这样?
答案 0 :(得分:2)
对于此例外,您必须编写ORDER BY "Columname" DESC
在这里,它会像
SELECT DISTINCT progressdate FROM entry WHERE progressdate >= date('now','start of month','-1 month') AND progressdate < date('now','start of month') ORDER BY progressdate desc
答案 1 :(得分:1)
您未在查询中使用任何Order By子句。也许您正试图按降序排列进度日期?
String query = "SELECT DISTINCT " + KEY_PROGRESSDATE + " FROM " + TABLE_ENTRY + " WHERE progressdate >= date('now','start of month','-1 month') AND progressdate < date('now','start of month') ORDER BY progressdate desc";
答案 2 :(得分:1)
如果函数已经存在,为什么要使用原始查询。尝试使用以下函数从任何表中获取值只需传递参数:
public String[] getValues(boolean b, String table, String column,
String where, String orderBy) {
ArrayList<String> savedAns = new ArrayList<String>();
Cursor result = null;
String[] y;
try {
result = db.query(b, table, new String[]{column}, where, null,
null, null, orderBy, null);
if (result.moveToFirst()) {
do {
savedAns.add(result.getString(result.getColumnIndex(column)));
} while (result.moveToNext());
} else {
return null;
}
y = savedAns.toArray(new String[result.getCount()]);
} finally {
result.close();
}
return y;
}
希望它可以帮助你..