如何使用C#删除SQLite数据库中超过30天的行?

huangapple go评论61阅读模式
英文:

How to delete rows in SQLite database where rows older than 30 days using c# ?

问题

使用C#删除SQLite数据表中lastVisited列为REAL类型的行,其中lastVisited列的数值比30天之前的数据旧(这是一个参数,可能是45天、60天或其他),该如何操作?

英文:

There is a SQLite data table with a lastVisited column which is REAL type .

I hope to delete rows using C# where lastVisited column are older than 30 days (This is a parameter, maybe it's 45, 60, or other), how can I do?

答案1

得分: 2

REAL 是支持的日期存储数据类型之一:

SQLite 没有为存储日期和/或时间设置专用的存储类。相反,SQLite 的内置日期和时间函数能够将日期和时间存储为 TEXT、REAL 或 INTEGER 值:

  • TEXT 以 ISO8601 字符串形式("YYYY-MM-DD HH:MM:SS.SSS")。
  • REAL 以朱利安日数表示,即格林威治时间的4714年11月24日中午以来的天数,根据普罗莱普提克·格里高利历法。
  • INTEGER 以Unix时间表示,即1970-01-01 00:00:00 UTC以来的秒数。

朱利安日数将在2400000到2500000范围内,您可以使用类似以下方式删除旧值:

DELETE ...
WHERE lastVisited < julianday('now') - 30
英文:

REAL is one the supported data type for storing dates :

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

Julian days would be in the 2400000..2500000 range, and you can delete old values with something like :

DELETE ...
WHERE lastVisited &lt; julianday(&#39;now&#39;) - 30

huangapple
  • 本文由 发表于 2023年2月6日 15:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定