在android的sqlite中存取DATETIME类型的方法。

      创建表时: 

String sql="create table tb3(idINTEGER PRIMARY KEY,timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, weight DOUBLE)";    
 
 timestamp字段缺省值是当前时间(基于GMT而不是local time)。这问题导致了记录的时间跟本地实际时间有几个小时的差距,费了我好大工夫才找到解决方法: 存的时候不变,取的时候根据自己所在时区调整时间。这是sqlite中的一个函数datetime所做的工作。 
读取时的方法:
Cursor cursor = db.rawQuery("datetime(timestamp,'localtime') from tb3",null);
String myDate =cursor.getString(cursor.getColumnIndex("datetime(timestamp,'localtime')"));
SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = format.parse(myDate);

    插入数据时,timestamp能自动生成.

相关文章:

  • 2021-11-29
  • 2021-11-26
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-11-30
  • 2021-11-30
猜你喜欢
  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2022-01-15
  • 2022-12-23
  • 2021-11-30
相关资源
相似解决方案