mardi 20 octobre 2015

How to update "datetime default current_timestamp" with ContentValues?

In an Android game user data (retrieved from different social networks) is stored in an SQLite table:

create table table_social (
        id text primary key,
        social integer not null, /* Facebook = 1, Google+ = 2, Twitter = 3, ... */
        first_name text not null,
        photo text null,
        city text null,
        stamp datetime default current_timestamp /* PROBLEM HERE */
);

The app user can set one of the profiles as "main" - displayed in the navigation drawer and to the remote game partners:

screenshot

In the above SQL table I would like to indicate that a profile is "main" by setting its "stamp" column to the newest timestamp.

And when I need the "main" profile I just call:

    Cursor cursor = getReadableDatabase().query(TABLE_SOCIAL,
        COLUMNS,
        null,
        null,
        null,
        null,
        "stamp desc",
        "1");

This works fine, but I have problems changing the "main" profile by updating the timestamp:

public void setMainUser(int social) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_STAMP, (String) null);
    getWritableDatabase().update(TABLE_SOCIAL, 
        values, 
        "social=?", 
        new String[]{ String.valueOf(social) });
}

I have also tried

    values.put(COLUMN_STAMP, 0);

but the timestamp update is still not happening.

Any suggestions please, how to trigger SQLite "default" rule for that column?

Aucun commentaire:

Enregistrer un commentaire