Qt中SQLite时间和日期的用法.docx

上传人:b****2 文档编号:2206662 上传时间:2023-05-02 格式:DOCX 页数:30 大小:258.47KB
下载 相关 举报
Qt中SQLite时间和日期的用法.docx_第1页
第1页 / 共30页
Qt中SQLite时间和日期的用法.docx_第2页
第2页 / 共30页
Qt中SQLite时间和日期的用法.docx_第3页
第3页 / 共30页
Qt中SQLite时间和日期的用法.docx_第4页
第4页 / 共30页
Qt中SQLite时间和日期的用法.docx_第5页
第5页 / 共30页
Qt中SQLite时间和日期的用法.docx_第6页
第6页 / 共30页
Qt中SQLite时间和日期的用法.docx_第7页
第7页 / 共30页
Qt中SQLite时间和日期的用法.docx_第8页
第8页 / 共30页
Qt中SQLite时间和日期的用法.docx_第9页
第9页 / 共30页
Qt中SQLite时间和日期的用法.docx_第10页
第10页 / 共30页
Qt中SQLite时间和日期的用法.docx_第11页
第11页 / 共30页
Qt中SQLite时间和日期的用法.docx_第12页
第12页 / 共30页
Qt中SQLite时间和日期的用法.docx_第13页
第13页 / 共30页
Qt中SQLite时间和日期的用法.docx_第14页
第14页 / 共30页
Qt中SQLite时间和日期的用法.docx_第15页
第15页 / 共30页
Qt中SQLite时间和日期的用法.docx_第16页
第16页 / 共30页
Qt中SQLite时间和日期的用法.docx_第17页
第17页 / 共30页
Qt中SQLite时间和日期的用法.docx_第18页
第18页 / 共30页
Qt中SQLite时间和日期的用法.docx_第19页
第19页 / 共30页
Qt中SQLite时间和日期的用法.docx_第20页
第20页 / 共30页
亲,该文档总共30页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Qt中SQLite时间和日期的用法.docx

《Qt中SQLite时间和日期的用法.docx》由会员分享,可在线阅读,更多相关《Qt中SQLite时间和日期的用法.docx(30页珍藏版)》请在冰点文库上搜索。

Qt中SQLite时间和日期的用法.docx

Qt中SQLite时间和日期的用法

SQLite时间的保存与查询

一时间的保存

经过GOOGLE发现大多数的解决方法为datetime.ToString("s")来解决的,经过测试此方法虽然解决的问题,但还不够完美。

因为这样格式化出来的时间在用工具SQLiteDeveloper查看时显示的时间看起来很怪,不直观。

而且如果在SQLiteDeveloper

手动修改了时间,在程序中会报错,因为这个时候保存的时间格式发现了改变。

经过测试发现datetime.ToString("yyyy-MM-dd

hh:

mm:

ss")可以很好的解决这个问题。

二时间的查询

如果你用SQLite作开发,一定少不了时间的查询,一定会让你动不少脑精。

因为和别的数据库不一样,就如要查询2009.3.20

11:

00:

00领取工资的有多少人的SQL怎么写呢,你一定会写成:

selectcount(*)fromTwherestatue='1'and[date]='2009-03-2011:

00:

00'

仔细查看会发现有问题,因为没有结果,实际表中是有结果的,这是为什么,其实我也不没有搞清楚。

这个问题还是在国外的一个

论坛发现解决方法的。

只要改一下上面的语句就可以了

selectcount(*)fromTwherestatue='1'anddatetime([date])=datetime('2009-03-2011:

00:

00')

or

selectcount(*)fromTwherestatue='1'anddatetime([date])='2009-03-2011:

00:

00'

记住2009-03-20不能写成为2009-3-20.

以上方法经过测目前没有发现问题,当然我也是初次使用SQLite来开发一个小项目,也许还有问题没有发现出来,请各位指教!

 

SQLite日期时间函数

SQLite并没有datatime字段类型,但是可以在字符串类型字段中存储时间,

并提供了一些比较实用的日期时间操作函数

strftime(日期时间格式,日期时间字符串,修正符,修正符,……)

strftime(日期时间格式,日期时间字符串)也就等价于AAuto中的:

time(日期时间字符串,日期时间格式),sqlite与AAuto使用的格式化语法也一样。

参考:

strftime()函数返回一个经过格式化的日期时间,

它可以用下面的符号对日期和时间进行格式化:

%d一月中的第几天01-31

%f小数形式的秒,SS.SSSS

%H小时00-24

%j一年中的第几天01-366

%JJulianDayNumbers

%m月份01-12

%M分钟00-59

%s从1970-01-01日开始计算的秒数

%S秒00-59

%w星期,0-6,0是星期天

%W一年中的第几周00-53

%Y年份0000-9999

%%%百分号

date,time,datetime,julianday函数

date(日期时间字符串,修正符,修正符,……)等价于strftime(“%Y-%m-%d”,…)

time(日期时间字符串,修正符,修正符,……)等价于strftime(“%H:

%M:

%S”,…)

datetime(日期时间字符串,修正符,修正符,……)等价于strftime(“%Y-%m-%d%H:

%M:

%S”,…)

julianday(日期时间字符串,修正符,修正符,……)等价于strftime(“%J”,…)

日期时间字符串

可以用以下几种格式:

格式有严格的要求2008-06-1503:

35:

28日期只能用'-'分隔,时间只能用':

'分隔,不足二位数的必须补零

1.[*]YYYY-MM-DD

2.[*]YYYY-MM-DDHH:

MM

3.[*]YYYY-MM-DDHH:

MM:

SS

4.[*]YYYY-MM-DDHH:

MM:

SS.SSS

5.[*]YYYY-MM-DDTHH:

MM

6.[*]YYYY-MM-DDTHH:

MM:

SS

7.[*]YYYY-MM-DDTHH:

MM:

SS.SSS

8.[*]HH:

MM

9.[*]HH:

MM:

SS

10.[*]HH:

MM:

SS.SSS

11.[*]now

12.[*]DDDD.DDDD

在第五种到第七种格式(ISO8601)中的“T”是一个分割日期和时间的字符;

第八种到第十种格式只代表2000-01-01日的时间,

第十一种格式的’now’表示返回一个当前的日期和时间,使用格林威治时间(UTC);

第十二种格式表示一个JulianDayNumbers。

修正符

日期和时间可以使用下面的修正符来更改日期或时间:

1.[*]NNNdays

2.[*]NNNhours

3.[*]NNNminutes

4.[*]NNN.NNNNseconds

5.[*]NNNmonths

6.[*]NNNyears

7.[*]startofmonth

8.[*]startofyear

9.[*]startofweek

10.[*]startofday

11.[*]weekdayN

12.[*]unixepoch

13.[*]localtime

14.[*]utc

前六个修正符就是简单的增加指定数值的时间和日期;第七到第十个修正符表示返回当前日期的开始;第十一个修正符表示返回下一个星期是N的日期和时间;第十二个修正符表示返回从1970-01-01开始算起的秒数;第十三个修正符表示返回本地时间。

下面举一些例子:

[*]计算机当前时间

SELECTdate(‘now’)

[*]计算机当前月份的最后一天

SELECTdate(‘now’,’startofmonth’,’+1month’,’-1day’)

[*]计算UNIX时间戳1092941466表示的日期和时间

SELECTdatetime(‘1092941466’,’unixepoch’)

[*]计算UNIX时间戳1092941466表示的本地日期和时间

SELECTdatetime(‘1092941466’,’unixepoch’,’localtime’)

[*]计算机当前UNIX时间戳

SELECTstrftime(‘%s’,’now’)

[*]两个日期之间相差多少天

SELECTjolianday(‘now’)-jolianday(‘1981-12-23’)

[*]两个日期时间之间相差多少秒

SELECTjulianday('now')*86400-julianday('2004-01-0102:

34:

56')*86400

[*]计算今年十月份第一个星期二的日期

SELECTdate('now','startofyear','+9months','weekday2');

[*]取大于现在时间的数据

select*from表where日期字段>datetime('now','localtime')

[*]比较日期指定部份,举一反三,同样使用strftime格式式日期来对日、周、年比较

select*from表wherestrftime('%m',日期字段)=strftime('%m','now')

[*]大于指定时间的第一条

selecttitle,pubtimefromarticlewherepubtime>'2008-06-1503:

35:

28'orderbypubtimeascLimit1Offset0

[*]小于指定时间的第一条

selecttitle,pubtimefromarticlewherepubtime<'2008-06-1503:

35:

28'orderbypubtimedescLimit1Offset0

简单示例:

SELECT

datetime(CHANGE_DATE,'localtime'),

strftime('%Y-%m-%d',CHANGE_DATE,'localtime'),

datetime('now','localtime'),

strftime('%Y-%m-%d','now','localtime'),

DATE('now','localtime'),

time('now','Localtime'),

time('2010-11-2701:

12:

21','Localtime','-8hour')asTime

FROMSALARY_HISTORY;

SELECT*FROMSALARY_HISTORYWHEREdate(CHANGE_DATE,'Localtime')=Date('now','Localtime')

SqliteWorkingwithDatesandTimes

Inoursampledatabasewehavechosentouseintegersforcolumnsthatstoreadatevalue,representedbytheformatYYYYMMDD.Thisformatisfairlyreadableand,becausethemostsignificantpart(theyear)comesfirst,allowsarithmeticcomparisonstobeperformed.ForinstancejustasFebruary29th2004isearlierthanMarch1st,20040229isasmallernumberthan20040301.Thistechniqueisnotwithoutitslimitations.First,thereisnovalidationonthevaluesstored.AlthoughFebruary29thisavaliddateintheleapyear2004,itdoesnotexistthreeyearsoutoffourandthevalue20050229isnotarealdate,yetcouldstillbestoredintheintegercolumnorcomparedtoarealdate.Infactevenifyouusedatriggertomakethenumbereightdigitslongandalsofallwithinasensibleyearrange,therearemanyvaluesthatcouldstillbestoredthatdonotrepresentdatesonthecalendar.Verystrictcheckingwouldberequiredinyourapplicationprogramtoensuresuchdateinformationwasvalid.Similarly,youcannotperformdatearithmeticusingintegerdates.Although20040101+7givesadatesevendayslater,20040330+7wouldgiveanumberthatlookslikeMarch37th.Wehavenotevenlookedatadatatypetostoreatimevalueyet,butthesamelimitationsapplyifanumericfieldisused.SQLitecontainsanumberoffunctionsthatallowyoutoworkwithbothdatesandtimesstoredascharacterstrings,allowingyoutomanipulatethevaluesinusefulways.ValidTimestringFormatsSQLiteisfairlyflexibleabouttheformatinwhichyoucanspecifyadateand/ortime.Thevalidtimestringformatsareshowninthefollowinglist:

[*]YYYY-MM-DD

[*]YYYY-MM-DDHH:

MM

[*]YYYY-MM-DDHH:

MM:

SS

[*]YYYY-MM-DDHH:

MM:

SS.SSS

[*]HH:

MM

[*]HH:

MM:

SS

[*]HH:

MM:

SS.SSS

[*]now

[*]DDDD.DDDD

Fortheformatstringsthatonlyspecifyatime,thedateisassumedtobe2000-01-01.Wherenotimeisspecified,middayisused.SimplyusingthestringnowtellsSQLitetousethecurrentdateandtime.TheformatstringDDDD.DDDDrepresentsaJuliandaynumberthenumberofdayssincenoononNovember24,4714BC,GreenwichMeanTime.SQLiteusesJuliandateformatinternallytomanipulatedateandtimevalues.DisplayingaFormattedDateandTimeThecoredateandtimefunctioninSQLiteisstrftime(),whichhasthefollowingprototype:

strftime(format,timestring,modifier,modifier,...)

ThisfunctionisbasedupontheCfunctionstrftime()andtheformatparameterwillacceptmost,althoughnotall,ofthesameconversionspecifiers.ThefollowingexampleshowshowadatecanbereformattedtoMM/DD/YYformatusingstrftime().sqlite>SELECTstrftime('%m/%d/%Y','2004-10-31');10/31/2004

Table3.3liststheconversionsthatcanbeperformedbySQLiteonatimestring.

Table3.3.DateandTimeConversionSpecifiersStringMeaning

%dDayofmonth,01-31

%fFractionalseconds,SS.SSS

%HHour,00-23

%jDayofyear,001-366

%JJuliandaynumber,DDDD.DDDD

%mMonth,00-12

%MMinute,00-59

%sSecondssince1970-01-01(unixepoch)

%SSeconds,00-59

%wDayofweek,0-6(0isSunday)

%WWeekofyear,01-53

%YYear,YYYY

%%%symbol

DateandTimeModifiersGivenoneormoreoptionalmodifierarguments,strftime()canperformacalculationonthedategivenintimestring.Toaddorsubtractaperiodoftime,thedays,hours,minutes,seconds,monthsandyearsmodifierscanbeused,asshownintheseexamples:

sqlite>SELECTstrftime('%Y-%m-%d','2004-10-31','+7days');2004-11-07sqlite>SELECTstrftime('%H:

%M','22:

00','+12hours');10:

00sqlite>SELECTstrftime('%Y-%m-%d%H:

%M:

%S','2004-01-0100:

00:

00','-1second','+1year');2004-12-3123:

59:

59

NoteThemodifierkeywordscanbewrittenaseithersingularorplural.Inthelastoftheprecedingexamples,weused1secondand1yearratherthan1secondsand1yearsforreadability.SQLitedoesnotunderstandEnglishgrammar,soeitherisalwaysacceptable.

IntheseexampleswehaveusedthesameoutputformatastheoriginaltimestringtoreturnthedateinformationinaformatthatcanberecognizedbySQLite.Youshouldonlyformatthedatedifferentlywhenyouwanttodisplayitinyourapplicationinaparticularway.Tosavehavingtoenterthesameformatstringsrepeatedlywhenworkingwithdates,SQLiteprovidesfourconveniencefunctionsthatcallstrftime()withpredefinedformats.Usedate()toreturnadatewiththeformatstring%Y-%m-%dandtime()toreturnatimeas%H:

%S.Thefunctiondatetime()returnsthedateandtimeusingthesetwoformatscombined.Finallyjulianday()usesthe%JformatspecifiertoreturntheJuliandaynumber.Theargumentstoallfourfunctionsarethesameasstrftime()exceptthattheformatargumentisomitted.Thefollowingexampleusesdatetime()toproduceamoreconciseSQLstatement:

sqlite>SELECTdatetime('2004-01-0100:

00:

00','-1second','+1year');2004-12-3123:

59:

59

Othermodifiersallowyoutoadjustadateortimetothenearestsignificantvalue.Specifyingstartofmonth,startofyear,orstartofdaywilldecreasethevaluegivenintimestringtomidnightonthefirstofthemonthoryear,oronthatdayrespectively.Whenexecutedonanydayduring2004,thestartofyearmodifierreturns2004-01-01,asshowninthefollowingexample:

sqlite>SELECTdatetime('now','startofyear');2004-01-0100:

00:

00

Modifiersareappliedtotimestringintheordertheyappearinthestatement,asshowninthefollowingexample.Notethathadthesecondstatementbeenexecutedonthelastdayofthemonth,theresultwouldhavebeendifferentthestartofthefollowingmonthwouldhavebeenreturned.sqlite>SELECTdatetime('now','startofmonth','+1day');2004-07-0200:

00:

00sqlite>SELECTdatetime('now','+1day','startofmonth');2004-07-0100:

00:

00

Anynumberofmodifierscanbecombined,givingyouconsiderablepowerwhenworkingwithdatesandtim

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2