程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Sqlite數據庫 >> 關於Sqlite >> SQLite3中的日期時間函數使用小結

SQLite3中的日期時間函數使用小結

編輯:關於Sqlite


復制代碼 代碼如下:
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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved