scarhand
scarhand

Reputation: 4337

retrieving the value of a column in a single row for sqlite

Trying to get the value of the "value" column where the "option" column is = "lastclick" in sqlite for my android app.

Here is my code for the function:

public String getLastClick() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT value FROM config where option='lastclick'", null);
    cursor.moveToFirst();
    return cursor.getString(0);
}

It does not seem to be working correctly... I am using the following code for a conditional statement and it doesn't ever recognize the string as being 0 even when I first install the app on the VD, my onCreate function inserts a row with a "lastclick" value of "0" into the database.

Anyway here is that code:

    DatabaseHandler db = new DatabaseHandler(this);
    String lastClick = db.getLastClick();

    // check if its the first time they've ever clicked
    if (lastClick != "0") {
        String millis = String.valueOf(Long.parseLong(dateString) - Long.parseLong(lastClick));
        db.insertRecord(millis);
    }

"option" and "value" are both varchar's.

Upvotes: 0

Views: 1487

Answers (1)

user
user

Reputation: 87074

The correct way to compare Strings is to use the equals method, so instead of :

if (lastClick != "0") {

write:

if (!lastClick.equals("0")) {

Upvotes: 2

Related Questions