SQL 如何将日期时间值插入 SQLite 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1933720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:49:03  来源:igfitidea点击:

How do I insert datetime value into a SQLite database?

sqldatetimesqlite

提问by Brad

I am trying to insert a datetime value into a SQLitedatabase. It seems to be sucsessful but when I try to retrieve the value there is an error:

我正在尝试将日期时间值插入到SQLite数据库中。似乎成功了,但是当我尝试检索该值时出现错误:

<Unable to read data>

<无法读取数据>

The SQL statements are:

SQL 语句是:

create table myTable (name varchar(25), myDate DATETIME)
insert into myTable (name,mydate) Values ('fred','jan 1 2009 13:22:15')

回答by itowlson

The format you need is:

你需要的格式是:

'2007-01-01 10:00:00'

i.e. yyyy-MM-dd HH:mm:ss

即 yyyy-MM-dd HH:mm:ss

If possible, however, use a parameterised query as this frees you from worrying about the formatting details.

但是,如果可能,请使用参数化查询,因为这使您无需担心格式细节。

回答by Tor Valamo

The way to store dates in SQLite is:

在 SQLite 中存储日期的方法是:

 yyyy-mm-dd hh:mm:ss.xxxxxx

SQLite also has some date and time functions you can use. See SQL As Understood By SQLite, Date And Time Functions.

SQLite 也有一些您可以使用的日期和时间函数。请参阅SQLite 所理解的 SQL,日期和时间函数

回答by OMG Ponies

You have to change the format of the date string you are supplying in order to be able to insert it using the STRFTIME function. Reason being, there is no option for a month abbreviation:

您必须更改您提供的日期字符串的格式,以便能够使用 STRFTIME 函数插入它。原因是,月份缩写没有选项:

%d  day of month: 00
%f  fractional seconds: SS.SSS
%H  hour: 00-24
%j  day of year: 001-366
%J  Julian day number
%m  month: 01-12
%M  minute: 00-59
%s  seconds since 1970-01-01
%S  seconds: 00-59
%w  day of week 0-6 with sunday==0
%W  week of year: 00-53
%Y  year: 0000-9999
%%  % 

The alternative is to format the date/time into an already accepted format:

另一种方法是将日期/时间格式化为已接受的格式:

  1. YYYY-MM-DD
  2. YYYY-MM-DD HH:MM
  3. YYYY-MM-DD HH:MM:SS
  4. YYYY-MM-DD HH:MM:SS.SSS
  5. YYYY-MM-DDTHH:MM
  6. YYYY-MM-DDTHH:MM:SS
  7. YYYY-MM-DDTHH:MM:SS.SSS
  8. HH:MM
  9. HH:MM:SS
  10. HH:MM:SS.SSS
  11. now
  1. YYYY-MM-DD
  2. YYYY-MM-DD HH:MM
  3. YYYY-MM-DD HH:MM:SS
  4. YYYY-MM-DD HH:MM:SS.SSS
  5. YYYY-MM-DDTHH:MM
  6. YYYY-MM-DDTHH:MM:SS
  7. YYYY-MM-DDTHH:MM:SS.SSS
  8. 时:分
  9. 时:分:秒
  10. 时:分:SS.SSS
  11. 现在

Reference: SQLite Date & Time functions

参考:SQLite 日期和时间函数

回答by freezing_

Use CURRENT_TIMESTAMPwhen you need it, instead OF NOW()(which is MySQL)

CURRENT_TIMESTAMP在需要时使用,而不是 OF NOW()(即 MySQL)

回答by Ali Amanzadegan

Read This: 1.2 Dateand Time Datatype best data type to store date and time is:

:1.2Date和时间数据类型最好的数据类型来存储日期和时间是:

TEXTbest format is: yyyy-MM-dd HH:mm:ss

TEXT最好的格式是: yyyy-MM-dd HH:mm:ss

Then read thispage; this is best explain about date and time in SQLite. I hope this help you

然后阅读页; 这是最好的解释日期和时间SQLite。我希望这对你有帮助

回答by Messy

This may not be the most popular or efficient method, but I tend to forgo strong datatypes in SQLitesince they are all essentially dumped in as strings anyway.

这可能不是最流行或最有效的方法,但我倾向于放弃SQLite 中的强数据类型,因为它们基本上都是作为字符串转储的。

I've written a thin C# wrapper around the SQLite library before (when using SQLite with C#, of course) to handle insertions and extractions to and from SQLite as if I were dealing with DateTime objects.

我之前(当然是在将 SQLite 与 C# 一起使用时)围绕 SQLite 库编写了一个薄的 C# 包装器来处理 SQLite 的插入和提取,就像我在处理 DateTime 对象一样。