get Max Id by table column name - Android Database

Android examples for Database:Table Row Query

Description

get Max Id by table column name

Demo Code


//package com.java2s;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Main {
    public static int getMaxId(SQLiteDatabase database, String tableName,
            String idColumnName) {
        int result = 0;
        String[] columns = { "MAX(" + idColumnName + ")" };
        Cursor resultCursor = database.query(tableName, columns, null,
                null, null, null, null);
        if (resultCursor.getCount() == 0) {
            return 0;
        }/*from w  w w  . j  a v  a 2s  .  com*/
        resultCursor.moveToFirst();
        result = resultCursor.getInt(0);
        resultCursor.close();
        return result;
    }

    public static Cursor query(SQLiteDatabase database, String tableName,
            String[] columns, String selection, String[] selectionArgs,
            String groupBy, String having, String orderBy, String limit) {
        return database.query(tableName, columns, selection, selectionArgs,
                groupBy, having, orderBy, limit);
    }
}

Related Tutorials