如何获得最后一个记录的SQLite的?如何获得、SQLite

2023-09-05 10:03:12 作者:一把感情刀

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

I have a one table question_table and one ImageButton (Back). I need to get the last inserted record from the database after clicking on the Back.

我行包含下列:问题 optionA optionB OptionC中 optionD ,我需要对使用的数据我活动。我在创建数据库中的一个方法,但它不工作。

My row contains the following columns: question, optionA, optionB, optionC, optionD, and I need the data for use on my Activity. I create one method in database but it's not working.

下面是code,以供参考:

Here is code for reference:

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();
        }           
    }
});

请给我一些暗示。 在此先感谢。

Please give me some hint. Thanks in Advance..

推荐答案

试试这个:

SELECT * 
    FROM    TABLE
    WHERE   ID = (SELECT MAX(ID)  FROM TABLE);

您还可以使用以下解决方法:

you can also used following solution:

SELECT * FROM表名ORDER BY列DESC LIMIT 1;

SELECT * FROM tablename ORDER BY column DESC LIMIT 1;