简体   繁体   中英

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:

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 . I tried various options but I can't get it to run.

What I need is an Android SQLite Statement for

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'

I tried the following rawQueries, none of them works:

In QuizDBHelper-Class

    //...
    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. I need ...newbie help!

I need something like 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. 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.

Rather it will will 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 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 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM