android - 如何从 Sqlite 获取最后一条记录?

我有一个表 question_table 和一个 ImageButton (Back)。单击Back 后,我需要从数据库中获取最后插入的记录。

我的行包含以下列:questionoptionAoptionBoptionCoptionD ,我需要在我的 Activity 上使用的数据。我在数据库中创建了一种方法,但它不起作用。

以下代码供引用:

MySQLiteHelper.java 提取:

public List<ObjectiveWiseQuestion> getLastInsertQuestion()
{
    // long index = 0;
    List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
    db = getReadableDatabase();
    Cursor cursor = db.query(
            "sqlite_sequence",
            new String[]{"seq"},
            "name = ?",
            new String[]{TABLE_QUESTION},
            null,
            null,
            null,
            null );

    if (cursor.moveToFirst())
    {
        do {
            ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();

            owq.setQuestion(cursor.getString(2));
            owq.setOptionA(cursor.getString(3));
            owq.setOptionB(cursor.getString(4));
            owq.setOptionC(cursor.getString(5));
            owq.setOptionD(cursor.getString(6));
            owq.setCorrectOption(cursor.getString(7));
            LocwiseProfileList.add(owq);
        } while(cursor.moveToNext());

        db.close();
    }

    return LocwiseProfileList;
}

OnClickListner 来自 AddQuestionActivity.java

imgBack.setOnClickListener( new View.OnClickListener() 
{                       
    @Override
    public void onClick(View v) 
    {
        msg();
        emptyFormField();

        try {
            final List<ObjectiveWiseQuestion> LocWiseProfile =  db.getLastInsertQuestion();       

            for (final ObjectiveWiseQuestion cn : LocWiseProfile)
            {   
                db=new MySQLiteHelper(getBaseContext());
                db.getWritableDatabase();
                txtQuestion.setText(cn.getQuestion());
                txtOptionA.setText(cn.getOptionA());
                txtOptionB.setText(cn.getOptionB());
                txtOptionC.setText(cn.getOptionC());
                txtOptionD.setText(cn.getOptionD());
                txtCorrectOption.setText(cn.getCorrectOption());
                db.close();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }           
    }
});

请给我一些提示。

最佳答案

我觉得上面的回答有点啰嗦,就用这个

SELECT * FROM table ORDER BY column DESC LIMIT 1;

https://stackoverflow.com/questions/9902394/

相关文章:

android - 在 Lollipop 崩溃前使用 android vector Drawable

android - 不同构建风格的不同应用名称?

android - 如何去除 ImageButton 的标准背景图片?

android - ADB - Android - 获取当前 Activity 的名称

android - 获取 android.content.res.Resources$NotFoun

android - 使用 Glide 库设置完成图像加载后进度条的可见性

android - 输入时清除 EditText 中的文本

android - 进行动态但方形布局的简单方法

Android - fragment 事务上的自定义动画未运行

android - 更改抽屉导航中菜单项的文本颜色