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:
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?
UPDATE:
As a workaround I have tried
values.put(COLUMN_STAMP, System.currentTimeMillis() / 1000);
but the record has not been updated for some reason (also I could not step through that part in Android studio debugger - wrong source code was showing there...)
A default value is used only when you're inserting a new row without a value for that column.
When updating a row, you must specifiy the new value:
db.execSQL("UPDATE "+TABLE_SOCIAL+
" SET "+COLUMN_STAMP+"=CURRENT_TIMESTAMP"+
" WHERE social=?",
new Object[]{ social });
(CURRENT_TIMESTAMP
is an SQL keyword, so you cannot use ContentValues
.)