Get table row count from SQLiteDatabase - Android Database

Android examples for Database:SQL Query

Description

Get table row count from SQLiteDatabase

Demo Code


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Main{
    private static final String TAG = "OmniataDBHelper";
    public static int size(SQLiteDatabase db, String name) {
        try {// w  w  w  .ja v a 2  s .  c om
            Cursor c = db.rawQuery("SELECT count(id) FROM " + name + ";",
                    null);
            c.moveToFirst();
            int sz = c.getInt(0);
            c.close();
            return sz;
        } catch (Exception e) {
            OmniataLog.e(TAG, e.getMessage());
            return 0;
        }
    }
}

Related Tutorials