0

I have to get all records count from table. My Approach is as follows:

  public int getTasksByStatusIDBetweenDates(int statusID, String startDate, String endDate) {

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(
            "select * from Task" +
                    " where StatusID=" + statusID + " and AssignDate between '" + startDate + "' and '" + endDate + "'", null);

    int total = 0;
    if (cursor.moveToFirst())
        total = cursor.getInt(0);

    cursor.close();
    return total;
}  

Currently it is returning me 0. My date format is "yyyy-MM-dd HH:mm:ss"

Please guide me where i am doing wrong. Thanks !

Noman
  • 4,049
  • 10
  • 38
  • 59

3 Answers3

2

Or to existing code just call cursor.getCount();

total=cursor.getCount();

Cursor.getCount();
Returns the numbers of rows in the cursor.

UPDATED
Query to get records b/w two date SO - How to select data between two date range in android SQLite

Query is
SELECT * FROM mytab where my_date BETWEEN '2016-03-01 00:00:00' AND '2016-03-19 00:00:00'

Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • Yes i tried that. Tell me 1 thing, i am using AssigneDate datatype as Text in table.. is it ok OR i have to use DATETIME data type ? – Noman Apr 21 '16 at 11:24
  • well for me TEXT type is working fine.. however try with DateTime also. – Bharatesh Apr 21 '16 at 11:28
  • Cool.. can you share your full function ? – Noman Apr 21 '16 at 11:29
  • Thanks i will try that.. i have to show in grid view ... records are for all months so i have to apply filters accordingly.. That is why i am passing date in query. – Noman Apr 21 '16 at 11:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109809/discussion-between-skadoosh-and-noman-hamid). – Bharatesh Apr 21 '16 at 12:40
0

I think you should be using COUNT(*) instead of just *.

Try this.

public int getTasksByStatusIDBetweenDates(int statusID, String startDate, String endDate) {

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(
            "select count(*) from Task" +
                    " where StatusID=" + statusID + " and AssignDate between '" + startDate + "' and '" + endDate + "'", null);

    int total = 0;
    if (cursor.moveToFirst())
        total = cursor.getInt(0);

    cursor.close();
    return total;
}  
ads
  • 1,703
  • 2
  • 18
  • 35
0

A query with SELECT * returns all columns of all rows of the table.

cursor.getInt(0) returns the value of the first column of the current row of the cursor.

To count rows, you could use a query with SELECT COUNT(*) ..., and read the value returned by that. Alternatively, use a helpful helper function that constructs the query for you:

public int getTasksByStatusIDBetweenDates(int statusID, String startDate, String endDate) {
    SQLiteDatabase db = this.getReadableDatabase();
    return DatabaseUtils.queryNumEntries(
            db, "Task",
            "StatusID=" + statusID + " and AssignDate between ? and ?",
            new String[]{ startDate, endDate });
}  
CL.
  • 173,858
  • 17
  • 217
  • 259