2011-09-07 94 views
0

SQLite的在存储时间比其他数据库不同的方法:如何选择SQLite的日期格式

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. 

什么是存储在一个SQLite数据库日期数据的最佳方式?。 TEXT,REAL还是INTEGER?
我感兴趣的东西,如“使用文本是空间占用空间,使用INTEGER是好的,但你会在2038年有很大的问题”

+0

你需要datetime或仅日期? – Tim

+0

datetime。长达数秒 – cprogrammer

回答

1

这是正确的吗?

 
Format Resolution  Min Year  Max Year  Bytes/Date 

Text Milliseconds 0 AD   9999 AD  23 or 46 
Real Milliseconds? 4713 BC   ????   8 
Integer Seconds   1970 AD   ????   8 

2
  • 因为SQLite的3.0所以今年整数存储在64位2038年不是一个真正的问题。
  • 使用REAL或INTEGER时,您必须在插入时以几分之几或几分之几的时间执行计算。使用Integer,分辨率将为1秒。用TEXT,它是一毫秒。

如果您担心总空间,并且不需要1970年之前的任何毫秒或日期,那么请使用INTEGER。

0

如果你不需要秒的小数部分的最简单的方法是划时代(之前或之后的秒整数1970-01-01 00:00)准确从-4714-11-24直到5352-11 -01 10点52分47秒

对于计算人类可读的日期还有就是功能strftime