title: Android and SQLite - retrieve max id of a table
body: <p>I am trying to create a method to retrieve the max id of a specific table.</p>
  
  <p>This is the code that doesn't work:</p>
  
  <pre><code>private long getMaxId()
  {
      String query = "SELECT MAX(id) AS max_id FROM mytable";
      Cursor cursor = db.rawQuery(query, new String[] {"max_id"});
  
      int id = 0;     
      if (cursor.moveToFirst())
      {
          do
          {           
              id = cursor.getInt(0);                  
          } while(cursor.moveToNext());           
      }
      return id;
  }
  </code></pre>
  
  <p>The exception being thrown is this:</p>
  
  <pre><code>E/AndroidRuntime(24624): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x200408
  </code></pre>
  
  <p>I suppose the problem is this line:</p>
  
  <pre><code>            id = cursor.getInt(0); 
  </code></pre>
  
  <p>Does anybody have an idea of how to fix this?</p>
  
  <p>Thanks.</p>