2015-10-20 67 views
0

在一个Android游戏的用户数据(来自不同社交网络中检索)被存储在一个SQLite表:如何使用ContentValues更新“datetime default current_timestamp”?

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 */ 
); 

该应用程序的用户可设置的配置文件作为“主”一个 - 在导航抽屉,并显示远程游戏合作伙伴:

screenshot

在上面的SQL表,我想指出一个文件为“主”,由它的“邮票”列设置为最新的时间戳。

当我需要的 “主” 档案I只要致电:

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

这工作得很好,但我必须通过更新时间戳的问题改变 “主” 的个人资料:

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) }); 
} 

我也试过

values.put(COLUMN_STAMP, 0); 

但是时间戳更新仍然没有发生。

请提出任何建议,如何触发该列的SQLite“默认”规则?

UPDATE:

正如我曾尝试

values.put(COLUMN_STAMP, System.currentTimeMillis()/1000); 

但记录尚未出于某种原因(我也无法通过Android Studio中的调试器部分步骤更新一种变通方法 - 错误源代码在那里显示...)

回答

1

默认值仅用于当您插入新的行而没有该列的值。

当更新行时,必须specifiy新值:

db.execSQL("UPDATE "+TABLE_SOCIAL+ 
      " SET "+COLUMN_STAMP+"=CURRENT_TIMESTAMP"+ 
      " WHERE social=?", 
      new Object[]{ social }); 

CURRENT_TIMESTAMP是一个SQL关键字,所以你不能使用ContentValues。)

相关问题