在SQLite数据库中插入当前日期和时间 [英] Inserting current date and time in SQLite database

查看:1054
本文介绍了在SQLite数据库中插入当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在SQLite中创建一个表,其中一个字段用于日期,当前实例的日期和时间应保存在该表中.我应该使用哪种数据类型?

我打算使用时间戳".如何在字段中插入当前时间戳值?还有如何为该日期字段编写内容值?

解决方案

SQLite支持标准SQL变量CURRENT_DATECURRENT_TIMECURRENT_TIMESTAMP:

INSERT INTO Date (LastModifiedTime) VALUES(CURRENT_TIMESTAMP)

SQLite中日期/时间的默认数据类型TEXT./p>

ContentValues不允许使用通用SQL表达式,只能使用固定值,因此您必须使用Java读取当前时间:

 cv.put("LastModifiedTime",
       new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
 

I want to create a table in SQLite in which one of the field is for date, in which date and time of current instance should save. Which data type should I use?

I'm planning to use 'timestamp'. How to insert current timestamp value to the field? Also how to write content values for this date field?

解决方案

SQLite supports the standard SQL variables CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP:

INSERT INTO Date (LastModifiedTime) VALUES(CURRENT_TIMESTAMP)

The default data type for dates/times in SQLite is TEXT.

ContentValues do not allow to use generic SQL expressions, only fixed values, so you have to read the current time in Java:

cv.put("LastModifiedTime",
       new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

这篇关于在SQLite数据库中插入当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆