在SQLite数据库中插入当前日期和时间 [英] Inserting current date and time in SQLite database
问题描述
我想在SQLite中创建一个表,其中一个字段用于日期,当前实例的日期和时间应保存在该表中.我应该使用哪种数据类型?
我打算使用时间戳".如何在字段中插入当前时间戳值?还有如何为该日期字段编写内容值?
SQLite支持标准SQL变量CURRENT_DATE
,CURRENT_TIME
和CURRENT_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屋!