3

I want to count rows of a table in android.I used 2 ways but not success & get a warning message

Launch timeout has expired, giving up wake lock!
 Activity idle timeout for HistoryRecord{44e26a30 com.india.screen/.CategoryList}

I have tried-

String query="Select count(*) from Holyplace_Tbl";
  1. Using rawQuery

    public void countRows(String query) {

    Cursor mcursor = assetDatabaseHelper.executeQuery(query);
    mcursor.moveToFirst();
    int icount = mcursor.getInt(0);
    System.out.println("NUMBER IN DB: " + icount);
    mcursor.close();
    

    }

  2. Using DatabaseUtils

    public int countRows(String query)
    {
    
       int sometotal = (int) DatabaseUtils.longForQuery(sqliteDatabaseObj, query, null);
      return sometotal;
     }
    

    AssetDatabaseOpenHelper.class

5 Answers 5

11

Try This

private static final String DB_TABLE_PLACES = "Places";
private SQLiteDatabase mDatabase;

private long fetchPlacesCount() {
    String sql = "SELECT COUNT(*) FROM " + DB_TABLE_PLACES;
    SQLiteStatement statement = mDatabase.compileStatement(sql);
    long count = statement.simpleQueryForLong();
    return count;
}

See More Here.

Sign up to request clarification or add additional context in comments.

21 Comments

I still have same warning message & get no value.
No, I Get it . see i have one tabel name is test with 2 rows .Perfectly run for me. see your code . use this method as public.I just copy this function in my db code and edit with public and call in main activity and print in toast. so may be any other problem in your code.
11-07 12:45:12.534: D/dalvikvm(92): GC freed 16379 objects / 524312 bytes in 55ms only these lines show in log if call function on button click,otherwise warning message
where you put this function ? and how did you call it ?
I put it in AssetDatabaseOpenHelper class
|
4

Simplest and Efficient way that i found from How to count number of records in sqlite

DatabaseUtils provides Static utility methods for dealing with databases and Cursors.

int numRows = DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM table_name", null);

Or:

int numRows = DatabaseUtils.queryNumEntries(db, "table_name");

2 Comments

queryNumEntries is meant for this!
Is "DatabaseUtils.queryNumEntries" still a good choice or has it been deprecated?
1

You can use a simple RawQuery like this:

private SQLiteDatabase mDatabase;

Cursor c = mDatabase.rawQuery("select COLUMN_NAME from TABLE_NAME",null);

int count = c.getCount();

The Cursor.getCount() method returns the number of rows in the cursor, whch in this case would be the number of items you are looking for.

5 Comments

Cursor.getCount() returns no of columns in cursor not row.
rows are horizontal and columns are vertical in a table. It returns no of rows. I'm sure you are mistaken. Please check again
@Mukesh. getCount definitely returns number of rows, Are you sure that you do not by coincidence have the same number of rows as you have columns?
@AnupCowkur yes,tables consists 200 to 400 rows
0
long numRows = DatabaseUtils.queryNumEntries(db, table);

1 Comment

Is "DatabaseUtils.queryNumEntries" still a good choice or has it been deprecated?
0

You can get row count as follows:

c = db.rawQuery("SELECT * FROM Your_tabename", null);
int i = c.getCount();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.