加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite 时间日期字段问题

发布时间:2020-12-12 20:34:09 所属栏目:百科 来源:网络整理
导读:1:建表的时候就设置好 CREATE TABLE testDate ( id INTEGER PRIMARY KEY AUTOINCREMENT, t TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE users( username varchar(20), userpwd varchar(20), createtime TIMESTAMP default (datetime('now','local

1:建表的时候就设置好

CREATE TABLE testDate (
id INTEGER PRIMARY KEY AUTOINCREMENT,
t TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE users(
username varchar(20),
userpwd varchar(20),
createtime TIMESTAMP default (datetime('now','localtime'))

)

2:利用SQLite内部函数

mDb.execSQL("INSERT INTO "+TABLE+" VALUES (datetime()) ");

datetime()函数要用引号包起来 当作字符串处理

3:直接把获得的date传给SQLite,格式不对,需要format下

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); ContentValues initialValues = new ContentValues(); initialValues.put("date_created",dateFormat.format(date)); long rowId = mDb.insert(DATABASE_TABLE,null,initialValues);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读