0

I have a method which should upgrade the unreadMessageCount of a given user.

public void increaseUnreadMessageCount(int userID) {
    String query = "UPDATE " + tableName + " SET " + DatabaseHelper.KEY_USER_UNREAD_MESSAGE_COUNT + " = "
            + DatabaseHelper.KEY_USER_UNREAD_MESSAGE_COUNT + " + 1 WHERE " + DatabaseHelper.KEY_USER_USER_ID
            + " = ?";
    Log.i("query", query);
    db.rawQuery(query, new String[] { String.valueOf(userID) });
}

This evaluates to: UPDATE user SET unreadPrivateMessageCount = unreadPrivateMessageCount + 1 WHERE userID = ? which seems correct to me. But somehow it is not working, the row is always 0.

Or is my SELECT statement wrong?

public int getUnreadPrivateMessageCount() {
    String query = "SELECT SUM(" + DatabaseHelper.KEY_USER_UNREAD_MESSAGE_COUNT + ") AS messageCount FROM "
            + tableName + " WHERE " + DatabaseHelper.KEY_USER_UNREAD_MESSAGE_COUNT + " <> ?";
    Log.i("query", query);
    Cursor cursor = db.rawQuery(query, new String[] { "0" });
    int messageCount = 0;
    if (cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            messageCount = cursor.getInt(cursor.getColumnIndexOrThrow("messageCount"));
            cursor.close();
        } else {
            Log.e("getUnreadPrivateMessageCount", "moveToFirst failed");
        }

    } else {
        Log.e("getUnreadPrivateMessageCount", "count 0");
    }

    return messageCount;
}

This query evaluates to SELECT SUM(unreadPrivateMessageCount) AS messageCount FROM user WHERE unreadPrivateMessageCount <> ? and no error log is triggered, which also seems correct to me.

2 Answers 2

1

rawQuery is used to execute queries, i.e., SELECT statements. To execute other SQL statements, use execSQL instead.

Also check if you really want to treat your IDs as strings.

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

3 Comments

Doc of execSQL : "Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE. "
@Chris The documentation is wrong; this function is not intended to execute those commands because there is no returned cursor (for SELECT) or there already are specialized functions (for the others).
Thanks, seems to work now. What's the purpose of life when the documentation is wrong :/
0

If your update syntax is correct then :

public void increaseUnreadMessageCount(int userID) {
    String query = "UPDATE " + tableName + " SET " + DatabaseHelper.KEY_USER_UNREAD_MESSAGE_COUNT + " = "
            + DatabaseHelper.KEY_USER_UNREAD_MESSAGE_COUNT + " + 1 WHERE " + DatabaseHelper.KEY_USER_USER_ID
            + " = ?";
    Log.i("query", query);
    db.rawQuery(query, new String[] { String.valueOf(userID) });
}

where did you use : getUnreadPrivateMessageCount(), no where, so it must be something like this :

public void increaseUnreadMessageCount(int userID) {
    String query = "UPDATE " + tableName + " SET " + DatabaseHelper.KEY_USER_UNREAD_MESSAGE_COUNT + " = "
            + getUnreadPrivateMessageCount() + " WHERE " + DatabaseHelper.KEY_USER_USER_ID
            + " = ?";
    Log.i("query", query);
    db.rawQuery(query, new String[] { String.valueOf(userID) });
}

In this way total unread message is passed to table.

1 Comment

The method is obviously called in another place.

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.