c# - 在 SQLite 中保存日期?

标签 c# sqlite datetime

我从这个链接找到了这个:

http://www.sqlite.org/datatype3.html

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").

REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.

INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.date and time functions.

我对应该使用哪个感到有点困惑。我正在创建一个 Windows 窗体 C# 应用程序,并将使用内置的 DateTime 控件。

哪个选项最适合我?我认为 TEXT 会,但也许我错了。我是 SQLite 新手。

谢谢。

最佳答案

我假设您使用的是 System.Data.SQLite ?即使 SQLite 本身无法识别日期时间数据类型,该数据提供程序也能识别它。

例如,如果您使用 SQLiteDataAdapter 通过 select 语句填充数据表,如果列的数据类型是日期时间,则返回的数据列将为日期时间。

提供程序有一个限制:当您的选择包含多个表时,它无法猜测数据类型。在这种情况下,您可以通过像这样在查询之前预先声明返回的数据类型:

types [integer], [text], [boolean], [datetime];
select A.id, A.subject, B.isactive, B.due_date from ...

提供程序将数据存储为文本,如 2009-04-01 17:42:38.828125。 SQLite 可以使用这种格式,例如,您可以使用以下方法计算第二天:

select datetime('2009-04-01 17:42:38.828125', '+1 days');

编辑:您将数据类型指定为日期时间,如下所示:

create table C ( d datetime );

关于c# - 在 SQLite 中保存日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063602/

相关文章:

c# - 并行化/多线程单例对象方法调用

ruby-on-rails - 运行 "rails server"时找不到 sqlite3 符号

sql - 对于相同的查询,SQLite命令行客户端和SQLite库之间的性能不同

python - 根据日期和时间分割数据

c# - 如何从字节数组创建位图?

c# - 无法将必备组件放在与我的应用程序相同的位置

vba - 在 Excel 中的 VBA 中访问 SQLite 数据库

python - 使用 pandas to_datetime 版本 0.23.4 获取今天的日期

c# - 将日期时间转换为没有默认时间值的字符串

c# - 字典作为参数,其中值类型无关紧要