开发者

How to format date and time values for SQLite?

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 pu开发者_Python百科t for A, B and C?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜