如何格式化 SQLite 的日期和时间值?

发布于 2024-10-02 21:30:17 字数 323 浏览 5 评论 0原文

不敢相信我必须在这里问这个。即使谷歌搜索也没有帮助。

我想这样做:

insert into atable (adatefield,atimefield,adatetimefield) values (A,B,C);
  • adatefield 被定义为 DATE
  • atimefield 被定义为 TIME
  • adatetimefield 被定义为 DATETIME

我该为 A、B 和 C 放置什么?

它是免费的,这很好,但是 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2024-10-09 21:30:17

日期、时间和日期时间字段实际上都会存储日期时间,这仅取决于您插入的内容,并且它们都将与日期字符串一起使用。

您可以

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

或者实际上可以将“2010-11-16 14:12:22”插入到所有字段中,然后使用以下命令将它们选择回来:

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

但是,如果您想确保字段仅包含确切的日期、时间或日期时间并且您从变量插入,那么您可以

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

Sqlites 无类型性使某些事情变得非常简单,但可能会使其他事情变得混乱或复杂化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文