Fabimaru
Fabimaru

Reputation: 493

rawQuery on "count(*)" return no row

I'm trying to fix a DB issue I had in Ankidroid. The following statement was executed:

SELECT count(*) FROM cards c WHERE type = 1 AND combinedDue <
1335153600.000000

It was generated there. The code executed is the following (github link):

cursor = mDatabase.rawQuery(query, null);
if (!cursor.moveToNext()) {
  throw new SQLException("No result for query: " + query);
}

I can't understand how I can get no record, I should get either 1 or 0. The call stack in the log was the following:

at com.ichi2.anki.Deck.rebuildRevCount(Deck.java:1351)
    Caused by: android.database.SQLException: No result for query: ……
at com.ichi2.anki.AnkiDb.queryScalar(AnkiDb.java:129)
at com.ichi2.anki.Deck._rebuildRevCount(Deck.java:1621)

Any idea?

In fact, I was more looking at a similar case. We have a table «Stats» with a column "global", in which there can be one global record and many daily records. If we don't find (a simple select) the global record, we create it. On my phone, it seems that sometime the global record is not found, so we create an additional one, which break things. It really look like the case I showed above.

Edit:

I found why. In another thread, an AsyncTask closes the DB at the same time the query is being made (because a lot of processing triggered by the GUI is done asynchronously). And it returns a cursor with no record.

I saw that by adding traces to a file.

Upvotes: 1

Views: 620

Answers (1)

Barak
Barak

Reputation: 16393

Try if (cursor.moveToFirst == false) instead of using the ! operator. I think that the way you are currently doing it is always going to.pass you into the body of the if. And the cursor isn' t properly initialized.without a.moveToFirst.

Upvotes: 1

Related Questions