如何为SQLite格式化日期和时间值? [英] How to format date and time values for SQLite?

查看:180
本文介绍了如何为SQLite格式化日期和时间值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不能相信我在这里要问。甚至谷歌搜索都没有帮助。



我想这样做:

  insert into atable(adatefield,atimefield,adatetimefield)values(A,B,C); 




  • adatefield定义为DATE

  • atimefield被定义为TIME

  • adatetimefield被定义为DATETIME



对于A,B和C?



很好,它是免费的,但SQLite的文档是可怕的。

解决方案

日期,时间和日期时间字段实际上都会存储datetime,它只取决于您插入的内容,并且都将使用日期字符串。



您可以将

 插入到无效值(2010-11-16,14:12 :22,2010-11-16 14:12:22); 

或者你实际上可以将2010-11-16 14:12:22插入所有字段,然后选择它们:

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

如果你想确保这些字段只包含一个日期,时间或日期时间,从变量插入,那么你可以将$($)

 插入到可变值(date(date),time(thedate)),datetime日期)); 

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 put 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.

这篇关于如何为SQLite格式化日期和时间值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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