How to format date and time values for SQLite?

雨燕双飞 提交于 2019-12-06 11:58:00

问题


Can't believe I have to ask this here. Even googling didn't help.

I want to do this:

insert into atable (adatefield,atimefield,adatetimefield) values (A,B,C);
  • adatefield is defined as DATE
  • atimefield is defined as TIME
  • adatetimefield is defined as DATETIME

What do I put for A, B and C?

It's nice that it's free but the documentation for SQLite is awful.


回答1:


A date, time and datetime field will actually all store datetime, it just depends on what you insert into it, and they will all work with date strings.

You can

insert into atable values("2010-11-16","14:12:22","2010-11-16 14:12:22");

or you can actually just insert "2010-11-16 14:12:22" into all the fields and then select them back with:

select date(adatefield), time(atimefield), datetime(adatetimefield) from atable;

If however you want to make sure that the fields only contain exactly a date,time or datetime and you're inserting from variables, then you can

insert into atable values(date(thedate),time(thedate),datetime(thedate));

Sqlites typelessness makes some things really easy, but can confuse or complicate some other things.



来源:https://stackoverflow.com/questions/4193241/how-to-format-date-and-time-values-for-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!