query Count row from database table - Android Database

Android examples for Database:SQL Query

Description

query Count row from database table

Demo Code


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

public class Main {
    public static int queryCount(SQLiteDatabase db, String tableName,
            String where, String[] whereArgs) {
        StringBuffer stringBuffer = new StringBuffer(
                "select count(*) from ");
        stringBuffer.append(tableName);//w ww  .  j av a 2 s.  c om
        if (where != null) {
            stringBuffer.append(" where ");
            stringBuffer.append(where);
        }
        Cursor cursor = db.rawQuery(stringBuffer.toString(), whereArgs);
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        return count;
    }
}

Related Tutorials