SQLite tutorial (built-in date and time functions)

SQLite database


1 Introduction:

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

2. Field type:

  1. NULL: null value

  2. INTEGER: a signed integer, stored in 1-4 bytes depending on the size of the value

  3. REAL: A floating-point number, stored as an 8-byte IEEE floating-point number

  4. TEXT: A text string, stored according to the database encoding (UTF-8/UTF-16BE/UTF-16LE)

  5. BLOB: A blob of data stored exactly according to its input.

More field types in other databases (such as MYSQL) correspond to the types of SQLite. Please refer to the official introduction of SQlite types . Friends who do not understand English, please refer to the Chinese translation of data types in SQLite .

3. Add, delete, check and modify operations and transactions of SQLite:

SQLite's addition, deletion, query, and modification operations are relatively common, and transaction usage is also relatively common, so I won't introduce them here. For more specific details, add, delete, check and modify the SQLite tutorial.

4. SQLite special data storage (emphasis):

  1. Store date and time data types:

    SQLite does not specifically provide date and time storage types, which can usually be stored in TEXT, REAL and INTEGER types instead.

    • Data corresponding to TEXT: "YYYY-MM-DD HH:MM:SS.SSS"format data

    • Data corresponding to REAL: Julian date format storage, that is, the number of days counted from the noon of GMT on November 24, 4714 BC.

    • Data corresponding to INTEGER: data in the form of Unix time, that is, the number of seconds from 1970-01-01 00:00:00 UTC.

    Dates and times can be stored in any of the above formats, and the different formats can be freely converted using the built-in date and time functions.

  2. Boolean data type:

    It is stored by replacing true with 1 and false with 0.

SQLite built-in date & time functions

SQLite supports the following five date and time functions, such as:

 date(timestring, modifier, modifier, ...)  : 返回YYYY-MM-DD 格式的日期

 time(timestring, modifier, modifier, ...) :  返回 HH:MM:SS格式的时间

 datetime(timestring, modifier, modifier, ...) 返回YYYY-MM-DD HH:MM:SS格式的日期时间

 julianday(timestring, modifier, modifier, ...): 返回从格林尼治时间的公元前 47141124 日正午算起的天数

 strftime(format, timestring, modifier, modifier, ...) :返回指定格式(即第一个参数)的日期

The above five date and time functions take a time string (TimeString) as a parameter. A time string followed by zero or more modifier modifiers. The strftime() function can also take the format string format as its first argument.

Note : the above functions can also be converted into strftime functions, as follows:

date(...)         strftime('%Y-%m-%d', ...)

time(...)         strftime('%H:%M:%S', ...)

datetime(...)     strftime('%Y-%m-%d %H:%M:%S', ...) 

julianday(...)    strftime('%J', ...)   

The different types of time strings, modifiers, and formats in the function will be explained in detail below.

  1. TimeString (time string) :

    A specific time with format (for example, 2017-04-31), the current time is replaced by now. More formats, as follows:

    • YYYY-MM-DD: For example, 2017-12-30
    • YYYY-MM-DD HH:MM: For example, 2017-12-30 12:10
    • YYYY-MM-DD HH:MM:SS.SSS For example: 2070-12-30 12:10:04.100
    • MM-DD-YYYY HH:MM For example: 30-12-2017 12:10
    • HH:MM For example: 12:10
    • YYYY-MM-DDTHH:MM 2017-12-30 12:10
    • HH:MM:SS 12:10:01
    • YYYYMMDD HHMMSS 20171230 121001
    • now : For example, the current time, 2017-4-9
  2. Modifier (modifier) :

     1. NNN days
     2. NNN hours
     3. NNN minutes
     4. NNN.NNNN seconds
     5. NNN months
     6. NNN years
     7. start of month
     8. start of year
     9. start of day
     10. weekday N  
     11. unixepoch
     12. localtime
     13. utc
    • From the 1st to the 6th, just add or subtract the specified number of times or dates, if NNN is negative, then subtract NNN times or dates. On the contrary, if NNN is positive, add;
    • The 7th to 9th are to set the specified date to the current month, year or day. The start of month is the current year-the current month-1 day, the start of year is the current year-January-1 day, and so on;
    • The 10th is to set the date to the next week N, where Sunday is 0;
    • The 11th is the unix timestamp converted into the corresponding date and time;
    • The 12th is the date and time converted to the local;

    Note : The order of modifiers is executed one by one from left to right, that is, the next one can only be executed after the previous one is executed.

  3. Format used in Strftime() :

格式   格式说明

%d  天数,例如:01-31中某一个天
%f  带小数部分(SS.SSS格式)的秒
%H  小时,例如: 00-23中某一个小时
%j  一年中的第几天,001-366
%J  儒略日数,DDDD.DDDD
%m  月,00-12中某一具体月份
%M  分,00-59
%s  从 1970-01-01 算起的秒数
%S  秒,00-59
%w  一周中的第几天,0-6 (0 is Sunday)
%W  一年中的第几周,01-53
%Y  年,YYYY
%%  % symbol

Using time and date functions in SQLite cases:


Find Sqlite.exe or SQLite under the tools file in the Android SDK to download and obtain. Use Sqlite3.exe to run sql statements.

Case 1: Get YYYY-MM-DDthe current date in the format, such as 2017-4-9

Analysis:
1. Use the date() function to return the date in YYYY-MM-DD format
2. Use the time string now to specify the current time

Therefore, this SQL statement is:SELECT date('now');

Case 2: Get the last day of the current month, such as 2017-4-30

Analysis:
1. Use the date() function to return the date in YYYY-MM-DD format
2. Use the time string (now) to specify the current time, and the date at this time is: current year-current month-current day
3. Use the modifier (start of month) to set the time as the current month, at this time the date is: current year-current month-1 day 4. Add a month, that is, the modifier (1 month), at this time the date is: current year-(current month+1)-1 day (- 1 day), get
to the last day of the month, at this time the date is: current year - current month - maximum number of
days

Therefore, this SQL statement is:SELECT date('now','start of month','+1 month','-1 day');

Case 3: Return 1970-01-01 00:00:00the number of seconds elapsed from the current time

Analysis:
1. Use the strftime() function
2. Use the second format, %s
3. Specify the time string (now)

Therefore, this SQL statement is:SELECT strftime('%s','now');

Case 4: Calculate the number of seconds since a certain moment in 2004

Analysis:
1. Calculate the speed per second up to 2004-01-01 02:34:56
2. Calculate the number of seconds up to today
3. Subtract the two.

Therefore, this SQL statement is:SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');

SQLite.exe executes sql, and the output results are as follows:

Write picture description here

Resource reference:

Guess you like

Origin blog.csdn.net/hexingen/article/details/71577318