create_index = 'CREATE INDEX IF NOT EXISTS idx_id ON test_table (id);'
cur.execute(create_table_stmt)
cur.execute(create_index)
conn.commit()
然后往里面插一點數(shù)據(jù)吧,SQLite只支持5種基本的數(shù)據(jù)類型
問題來了,SQLite的時間和日期類型在哪里?原來SQLite可以把時間日期保存在一下幾種數(shù)據(jù)類型里面
insert_stmt = 'insert into test_table values (?, ?, ?)'
record = (123, '2011-11-30 12:34:56', 'hello world')
cur.execute( insert_stmt, record )
conn.commit()
查看表結(jié)構(gòu) select * from sqlite_master
查看表信息 PRAGMA table_info (table_name)
SQLite中的時間日期函數(shù)
SQLite包含了如下時間/日期函數(shù):
datetime()的用法是:datetime(日期/時間,修正符,修正符...)
date()和time()的語法與datetime()相同。
在時間/日期函數(shù)里可以使用如下格式的字符串作為參數(shù):
舉例(寫這個筆記的時間是2006年10月17日晚8點到10點,北京時間):
select datetime('2006-10-17');
結(jié)果:2006-10-17 12:00:00
select datetime('2006-10-17 00:20:00', '+1 hour', '-12 minute');
結(jié)果:2006-10-17 01:08:00
select date('2006-10-17', '+1 day', '+1 year');
結(jié)果:2007-10-18
select datetime('now', 'start of year');
結(jié)果:2006-01-01 00:00:00
select datetime('now', 'start of month');
結(jié)果:2006-10-01 00:00:00
select datetime('now', 'start of day');
結(jié)果:2006-10-17 00:00:00
# 盡管第2個參數(shù)加上了10個小時,但是卻被第3個參數(shù) start of day 把時間歸零到00:00:00
# 隨后的第4個參數(shù)在00:00:00的基礎(chǔ)上把時間增加了10個小時變成了10:00:00。
select datetime('now', '+10 hour', 'start of day', '+10 hour');
結(jié)果:2006-10-17 10:00:00
# 把格林威治時區(qū)轉(zhuǎn)換成本地時區(qū)。
select datetime('now', 'localtime');
結(jié)果:2006-10-17 21:21:47
select datetime('now', '+8 hour');
結(jié)果:2006-10-17 21:24:45
它可以用以下的符號對日期和時間進行格式化:
%d 月份, 01-31
%f 小數(shù)形式的秒,SS.SSS
%H 小時, 00-23
%j 算出某一天是該年的第幾天,001-366
%m 月份,00-12
%M 分鐘, 00-59
%s 從1970年1月1日到現(xiàn)在的秒數(shù)
%S 秒, 00-59
%w 星期, 0-6 (0是星期天)
%W 算出某一天屬于該年的第幾周, 01-53
%Y 年, YYYY
%% 百分號
strftime() 的用法舉例如下:
|
新聞熱點
疑難解答
圖片精選