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

2023-12-04 0 891

复制代码 代码如下:import sqlite3conn = 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 valueREAL.The value is a floating point value, stored as an 8-byte IEEE floating point numberTEXT.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函数例如找前一天存进去的数据:复制代码 代码如下:SELECTid,duration,event_date,parameterFROM test_tableWHEREDATE(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-DDYYYY-MM-DD HH:MMYYYY-MM-DD HH:MM:SSHH:MMHH:MM:SSnow # 其中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:45strftime() 函数可以把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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 SQLite SQLite3中的日期时间函数使用小结 https://www.u-9.cn/database/sqlite/67444.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务