title: How to get Last record from Sqlite?
body: <p>I have a one table <code>question_table</code> and one <code>ImageButton</code> (<em>Back</em>). I need to get the last inserted record from the database after clicking on the <em>Back</em>. </p>
  
  <p>My row contains the following columns: <code>question</code>, <code>optionA</code>, <code>optionB</code>, <code>optionC</code>, <code>optionD</code>, and I need the data for use on my <code>Activity</code>. I create one method in database but it's not working.  </p>
  
  <p>Here is code for reference: </p>
  
  <p><em>MySQLiteHelper.java</em> extract:</p>
  
  <pre><code>public List&lt;ObjectiveWiseQuestion&gt; getLastInsertQuestion()
  {
      // long index = 0;
      List&lt;ObjectiveWiseQuestion&gt;LocwiseProfileList=new ArrayList&lt;ObjectiveWiseQuestion&gt;();
      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;
  }
  </code></pre>
  
  <p><code>OnClickListner</code> from <em>AddQuestionActivity.java</em></p>
  
  <pre><code>imgBack.setOnClickListener( new View.OnClickListener() 
  {                       
      @Override
      public void onClick(View v) 
      {
          msg();
          emptyFormField();
  
          try {
              final List&lt;ObjectiveWiseQuestion&gt; 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();
          }           
      }
  });
  </code></pre>
  
  <p>Please give me some hint.  </p>