Ginbak
Ginbak

Reputation: 105

Android sqlite - cursor count not 0 on empty table

I have the following code in a bigger project:

final class DBlifetimeStatisticsHandler{ //implements DBvalueHandler<Cyclist, Double>{

private  final String TAG = getClass().getName();
private static final boolean debug = true;

private final DBminMaxAvgHandler dbMinMaxAvgHandler = new DBminMaxAvgHandler();

// table name
private static final String TABLE_LIFETIME_STATISTICS = "lifetime_statistics";

// column names
private static final String KEY_LIFETIME_STATISTICS_ID = "lifetime_statistics_id";
private static final String KEY_MIN_MAX_AVG = "min_max_avg";

// table create statement
private static final String CREATE_TABLE = "CREATE TABLE "
        + TABLE_LIFETIME_STATISTICS + "(" 
        + KEY_LIFETIME_STATISTICS_ID + " LONG PRIMARY KEY NOT NULL," 
        + KEY_MIN_MAX_AVG   + " LONG"
        + ")";

public void onCreateTable(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}

public void onUpgrade(SQLiteDatabase db) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIFETIME_STATISTICS);
    onCreateTable(db);
}

public long addValue(SQLiteDatabase db, Statistics Statistics ) {
    ContentValues values = new ContentValues();

    long ID = getLatestID(db)+1;

    values.put(KEY_STATISTICS_ID, ID);

    ... //not important to the question

}

private long getLatestID(SQLiteDatabase db){
    String selectQuery = "SELECT  MAX(" + KEY_STATISTICS_ID +") FROM " + TABLE_STATISTICS;

    Cursor c = db.rawQuery(selectQuery, null);

    c.moveToFirst();
    int id = 0;
    Log.e("count", String.valueOf(c.getCount()));

    if (c.moveToFirst()){
       ...
    }

    return id;
}
}

After I updated the table it is created again. So when I try to add a new value I had problems cause it always jumped into the if clause because c.moveToFirst() always returned true.

So I tried to tried to check if c.getCount() would return true but sadly it does always return 1. So the question is: Why would it return 1 on an empty table? (I do use Questoid SQLite Browser and the table is really empty)

Upvotes: 2

Views: 1837

Answers (6)

silipy
silipy

Reputation: 11

this is my solution

public Boolean isNotEmpty(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT  * FROM " + TABLE_STATISTICS, null);
    while (cursor.moveToNext() ) {
        return true;
    }
    return false;
}

Upvotes: 1

Peter
Peter

Reputation: 1348

I resolve this probme this way:

 SELECT COUNT(*) AS numero, MAX(tagua_lagps) as tmp_max_lagps, MAX(tagua_logps) as tmp_max_logps, MIN(tagua_lagps) as tmp_min_lagps, MIN(tagua_logps) as tmp_min_logps FROM TAB_AGUA

On empty table, c.getCount(); gives 1 but values are NULL. But numero (c.getString(c.getColumnIndex("numero")) has a value of 0.

So rather than checking c.getCount() you must check the result of count(*).

Upvotes: 0

SANAT
SANAT

Reputation: 9267

I am using group by to resolve this. Please check my example :

SELECT  COUNT(*) FROM " + TABLE_NAME + " WHERE isSynced=0 group by isSynced

Upvotes: 0

Michael Krause
Michael Krause

Reputation: 4849

You are getting a result with one row in your Cursor because that is what you requested.

The result is a single column called MAX with a value that will be the max id of all the rows in your table. In your case of an empty table, this value is null.

Upvotes: 0

Rimas
Rimas

Reputation: 6024

You use aggregate function MAX, so read documentation:

There are two types of simple SELECT statement - aggregate and non-aggregate queries. A simple SELECT statement is an aggregate query if it contains either a GROUP BY clause or one or more aggregate functions in the result-set.

An aggregate query without a GROUP BY clause always returns exactly one row of data, even if there are zero rows of input data.

Upvotes: 7

Pavlos
Pavlos

Reputation: 2181

It might be some kind of a buggy behavior when using MAX. Check this link too Android database (SQLite) returns a non-empty cursor from an empty table

Upvotes: 2

Related Questions