SQLite3中的日期时间函数使用小结

SQLite3中的日期时间函数使用小结


复制代码 代码如下:

import sqlite3
conn = sqlite3.connect('/tmp/sqlite.db')
cur = conn.cursor()

接下来干嘛呢?建一张表吧。这里需要注意的是,SQLite不支持在创建表的同时创建索引,所以要分两步走,先创建表然后再创建索引
复制代码 代码如下:
create_table_stmt = '''CREATE TABLE IF NOT EXISTS test_table (
 id INTEGER PRIMARY KEY AUTOINCREMENT,
 duration INTEGER,
 event_date TEXT,
 parameter TEXT );'''

create_index = 'CREATE INDEX IF NOT EXISTS idx_id ON test_table (id);'
cur.execute(create_table_stmt)
cur.execute(create_index)
conn.commit()

然后往里面插一点数据吧,SQLite只支持5种基本的数据类型

复制代码 代码如下:

NULL. The value is a NULL value    
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value
REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number
TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE)
BLOB. The value is a blob of data, stored exactly as it was input

问题来了,SQLite的时间和日期类型在哪里?原来SQLite可以把时间日期保存在一下几种数据类型里面

复制代码 代码如下:

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.

insert_stmt = 'insert into test_table values (?, ?, ?)'
record = (123, '2011-11-30 12:34:56', 'hello world')
cur.execute( insert_stmt, record )
conn.commit()


把日期保存为字符串以后,不能直接拿出来直接当日期用,在用之前要调用SQLite的date函数
例如找前一天存进去的数据:
复制代码 代码如下:

SELECT
 id,
 duration,
 event_date,
 parameter
 FROM test_table
WHERE
 DATE(event_date) = DATE('now', '-1 day', 'localtime')
ORDER BY id, event_date

查看表结构 select * from sqlite_master
查看表信息 PRAGMA table_info (table_name)

SQLite中的时间日期函数

SQLite包含了如下时间/日期函数:

复制代码 代码如下:

datetime() .......................  产生日期和时间
date()  ........................... 产生日期
time()  ........................... 产生时间
strftime() .......................  对以上三个函数产生的日期和时间进行格式化

datetime()的用法是:datetime(日期/时间,修正符,修正符...)
date()和time()的语法与datetime()相同。

在时间/日期函数里可以使用如下格式的字符串作为参数:

复制代码 代码如下:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
HH:MM
HH:MM:SS
now                                 # 其中now是产生现在的时间。

举例(写这个笔记的时间是2006年10月17日晚8点到10点,北京时间):

复制代码 代码如下:

select datetime('now');
结果:2006-10-17 12:55:54

select datetime('2006-10-17');
结果:2006-10-17 12:00:00

select datetime('2006-10-17 00:20:00', '+1 hour', '-12 minute');
结果:2006-10-17 01:08:00

select date('2006-10-17', '+1 day', '+1 year');
结果:2007-10-18

select datetime('now', 'start of year');
结果:2006-01-01 00:00:00

select datetime('now', 'start of month');
结果:2006-10-01 00:00:00

select datetime('now', 'start of day');
结果:2006-10-17 00:00:00

# 尽管第2个参数加上了10个小时,但是却被第3个参数 start of day 把时间归零到00:00:00
# 随后的第4个参数在00:00:00的基础上把时间增加了10个小时变成了10:00:00。
select datetime('now', '+10 hour', 'start of day', '+10 hour');
结果:2006-10-17 10:00:00

# 把格林威治时区转换成本地时区。
select datetime('now', 'localtime');
结果:2006-10-17 21:21:47

select datetime('now', '+8 hour');
结果:2006-10-17 21:24:45


strftime() 函数可以把YYYY-MM-DD HH:MM:SS格式的日期字符串转换成其它形式的字符串。
strftime() 的语法是strftime(格式, 日期/时间, 修正符, 修正符, ...)

它可以用以下的符号对日期和时间进行格式化:
%d 月份, 01-31
%f 小数形式的秒,SS.SSS
%H 小时, 00-23
%j 算出某一天是该年的第几天,001-366
%m 月份,00-12
%M 分钟, 00-59
%s 从1970年1月1日到现在的秒数
%S 秒, 00-59
%w 星期, 0-6 (0是星期天)
%W 算出某一天属于该年的第几周, 01-53
%Y 年, YYYY
%% 百分号

strftime() 的用法举例如下:

复制代码 代码如下:

select strftime('%Y/%m/%d %H:%M:%S', 'now', 'localtime');

结果:2006/10/17 21:41:09