Skip to content Skip to sidebar Skip to footer

Get Max Id Row Of A Table

My question is about how get the max id row of a table... I'm using max function but give me a error Here is my code public static long getLastIdQuotaAdded(Context context){

Solution 1:

there is a table named sqlite_sequence in SQLITE that is used to store the auto_increment_key.

this is the query to get latest auto_increment_key values.

Cursorcursor= db.rawQuery("SELECT seq FROM sqlite_sequence WHERE name=?",
                newString[] { TABLE_NAME });
intlast= (cursor.moveToFirst() ? cursor.getInt(0) : 0);

Solution 2:

Your query (even the part that is visible) is not valid SQL.

To get the maximum value of a specific column, use something like this:

SELECTMAX(_id) FROM mytable;

In SQLite, if your ID is the Row ID (see the documentation), you can just do:

SELECT last_insert_rowid();

Solution 3:

Here is my solution to get max id of a table....

publicstatic long getLastIdQuotaAdded(Context context){

    Cursor cursor;
    String selection = null;
    String[] selectionArgs = null;
    selection = "SELECT MAX("+C_ID+") FROM sessaoquota";

    cursor=context.getContentResolver().query(URI, ALL_COLUMNS, selection, selectionArgs, null);

    if(cursor.moveToFirst())
        do{
            idMax = cursor.getInt((0));
        }while(cursor.moveToNext());

    return idMax;
}

Post a Comment for "Get Max Id Row Of A Table"