[英]SQLite Statement in Android Studio 3.5.3
I'm a newbie with Android Studio so please be patient... This forum often leads me with suggestions and examples (as a reader), but today I decided to ask for help:我是 Android Studio 的新手,所以请耐心等待……这个论坛经常给我提供建议和示例(作为读者),但今天我决定寻求帮助:
Since hours, I try to build an SQLite statement in Android Studio: There is a column COLUMN_LAST_ATTEMPT
with date and time as String, eg 2020-01-09 17:23
, see screenshot, and I want to get the newest date (without time) from the table, eg 2020-09-01
.从几小时开始,我尝试在 Android Studio 中构建 SQLite 语句:有一列
COLUMN_LAST_ATTEMPT
将日期和时间作为字符串,例如2020-01-09 17:23
,请参阅屏幕截图,我想获取最新日期(没有时间) 来自表,例如2020-09-01
。 I tried various options but I can't get it to run.我尝试了各种选项,但无法运行。
What I need is an Android SQLite Statement for我需要的是一个 Android SQLite 语句
SELECT MAX(SUBSTR(last_attempt,11,20)) FROM quiz_questions
(which runs on DBBrowser), where 'last attempt' is a column of table 'quiz_questions', screenshot of that column in table 'quiz_questions' (在 DBBrowser 上运行),其中“最后一次尝试”是“quiz_questions”表中的一列,“quiz_questions”表中该列的屏幕截图
I tried the following rawQueries, none of them works:我尝试了以下 rawQueries,它们都不起作用:
In QuizDBHelper-Class在 QuizDBHelper 类中
//...
final QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
//...
public String newestQuiz(){
db = getReadableDatabase();
String result = null;
Cursor cursor = db.rawQuery("SELECT MAX(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + ") FROM "
+ QuizContract.QuestionsTable.TABLE_NAME, null);
//Cursor cursor = db.rawQuery("SELECT MAX(SUBSTR(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT +
// ",11,20)) FROM " + QuizContract.QuestionsTable.TABLE_NAME, null);
//Cursor cursor = db.rawQuery("SELECT " + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + " FROM " +
// QuizContract.QuestionsTable.TABLE_NAME, null);
if(cursor.moveToFirst()){
do {
result = cursor.getString(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT));
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
In Statistics-Class在统计课
String LastUse = dbHelper.newestQuiz();
LastUsage.setText("Letzte Challenge: " + LastUse);
//LastUsage is a TextView in activity_Statistics.xml
//attached with LastUsage = findViewById(R.id.text_lastUsage);
Either the SQLite statements are totally wrong or I make (basic?) mistakes in statistics class. SQLite 语句要么完全错误,要么我在统计课上犯了(基本的?)错误。 I need ...newbie help!
我需要...新手帮助!
I need something like Select column from table where substring of date-Entry == newest
我需要类似
Select column from table where substring of date-Entry == newest
Your issue appear to be column names.您的问题似乎是列名。 That is a Cursor only contains the columns extracted, not all the columns from the table.
那是一个 Cursor 只包含提取的列,而不是表中的所有列。 Although you are basing your query on the column as per
QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT
that will not be the column name in the cursor.尽管您根据
QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT
将查询基于列, QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT
不会是游标中的列名。
Rather it will will MAX(SUBSTR(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + // ",11,20))
相反,它将
MAX(SUBSTR(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + // ",11,20))
The simplest way of managing this is to give the column in the Cursor a specific name using AS .最简单的管理方法是使用AS为 Cursor 中的列指定一个特定名称。 As such perhaps use :-
因此,也许使用:-
Cursor cursor = db.rawQuery("SELECT MAX(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + ") AS " + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + " FROM "
+ QuizContract.QuestionsTable.TABLE_NAME, null);
However, you may prefere to use a column name (AS ????) specififc to the situation eg但是,您可能更喜欢使用特定于情况的列名(AS ????),例如
........ AS max_" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + ........
You would then have to use :-然后你将不得不使用:-
result = cursor.getString(c.getColumnIndex("max_" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT));
Alternately, as it's just a single value/column that is returned in the cursor you could use the column offset of 0, in which case the column name is irrelevant as long as it is valid.或者,由于它只是在游标中返回的单个值/列,因此您可以使用列偏移量 0,在这种情况下,只要有效,列名就无关紧要。 However, using offsets is not typically recommended due to the lack of validation of the column being accessed.
但是,由于缺乏对正在访问的列的验证,通常不建议使用偏移量。
re the comment :-重新评论:-
I just need the date part
我只需要日期部分
As the date is a recognised DateTime format (and also that such formats are directly sortable/orderable), use max(date(column_name)) or even max(column_name).由于日期是公认的日期时间格式(并且此类格式可直接排序/排序),因此请使用 max(date(column_name)) 甚至 max(column_name)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.