Qt中SQLite时间和日期的用法.docx_第1页
Qt中SQLite时间和日期的用法.docx_第2页
Qt中SQLite时间和日期的用法.docx_第3页
Qt中SQLite时间和日期的用法.docx_第4页
Qt中SQLite时间和日期的用法.docx_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SQLite 时间的保存与查询一 时间的保存经过GOOGLE发现大多数的解决方法为datetime.ToString(s) 来解决的,经过测试此方法虽然解决的问题,但还不够完美。因为这样格式化出来的时间在用工具SQLite Developer 查看时显示的时间看起来很怪,不直观。而且如果在SQLite Developer手动修改了时间,在程序中会报错,因为这个时候保存的时间格式发现了改变。经过测试发现datetime.ToString(yyyy-MM-dd hh:mm:ss)可以很好的解决这个问题。二 时间的查询如果你用SQLite作开发,一定少不了时间的查询,一定会让你动不少脑精。因为和别的数据库不一样,就如要查询2009.3.2011:00:00领取工资的有多少人的SQL怎么写呢,你一定会写成:select count(*) from T where statue=1 and date=2009-03-20 11:00:00仔细查看会发现有问题,因为没有结果,实际表中是有结果的,这是为什么,其实我也不没有搞清楚。这个问题还是在国外的一个论坛发现解决方法的。只要改一下上面的语句就可以了select count(*) from T where statue=1 and datetime(date)=datetime(2009-03-20 11:00:00)or select count(*) from T where statue=1 and datetime(date)=2009-03-20 11:00:00记住2009-03-20不能写成为2009-3-20.以上方法经过测目前没有发现问题,当然我也是初次使用SQLite来开发一个小项目,也许还有问题没有发现出来,请各位指教!SQLite 日期时间函数SQLite并没有datatime字段类型,但是可以在字符串类型字段中存储时间,并提供了一些比较实用的日期时间操作函数strftime(日期时间格式, 日期时间字符串, 修正符, 修正符, ) strftime( 日期时间格式, 日期时间字符串 ) 也就等价于AAuto中的: time( 日期时间字符串,日期时间格式 ) ,sqlite与AAuto 使用的格式化语法也一样。参考:/doc/reference/libraries/kernel/time/time.htmlstrftime() 函数返回一个经过格式化的日期时间,它可以用下面的符号对日期和时间进行格式化:%d 一月中的第几天 01-31%f 小数形式的秒,SS.SSSS%H 小时 00-24%j 一年中的第几天 01-366%J Julian Day Numbers%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-15 03:35:28 日期只能用-分隔,时间只能用: 分隔,不足二位数的必须补零1.*YYYY-MM-DD2.*YYYY-MM-DD HH:MM3.*YYYY-MM-DD HH:MM:SS4.*YYYY-MM-DD HH:MM:SS.SSS5.*YYYY-MM-DDTHH:MM6.*YYYY-MM-DDTHH:MM:SS7.*YYYY-MM-DDTHH:MM:SS.SSS8.*HH:MM9.*HH:MM:SS10.*HH:MM:SS.SSS11.*now12.*DDDD.DDDD在第五种到第七种格式(ISO8601)中的“T”是一个分割日期和时间的字符;第八种到第十种格式只代表2000-01-01日的时间,第十一种格式的now表示返回一个当前的日期和时间,使用格林威治时间(UTC);第十二种格式表示一个 Julian Day Numbers。修正符日期和时间可以使用下面的修正符来更改日期或时间:1.*NNN days2.*NNN hours3.*NNN minutes4.*NNN.NNNN seconds5.*NNN months6.*NNN years7.*start of month8.*start of year9.*start of week10.*start of day11.*weekday N12.*unixepoch13.*localtime14.*utc前六个修正符就是简单的增加指定数值的时间和日期;第七到第十个修正符表示返回当前日期的开始;第十一个修正符表示返回下一个星期是N的日期和时间;第十二个修正符表示返回从1970-01-01开始算起的秒数;第十三个修正符表示返回本地时间。下面举一些例子:*计算机当前时间SELECT date(now)*计算机当前月份的最后一天SELECT date(now,start of month,+1 month,-1 day)*计算UNIX 时间戳1092941466表示的日期和时间SELECT datetime(1092941466,unixepoch)*计算 UNIX 时间戳1092941466 表示的本地日期和时间SELECT datetime(1092941466,unixepoch,localtime)*计算机当前UNIX 时间戳SELECT strftime(%s,now)*两个日期之间相差多少天SELECT jolianday(now)-jolianday(1981-12-23)*两个日期时间之间相差多少秒SELECT julianday(now)*86400 - julianday(2004-01-01 02:34:56)*86400*计算今年十月份第一个星期二的日期SELECT date(now,start of year,+9 months,weekday 2);*取大于现在时间的数据select * from 表 where 日期字段datetime(now,localtime)*比较日期指定部份,举一反三,同样使用strftime格式式日期来对日、周、年比较 select * from 表 where strftime(%m,日期字段)=strftime(%m,now)*大于指定时间的第一条 select title,pubtime from article where pubtime2008-06-15 03:35:28 order by pubtime asc Limit 1 Offset 0*小于指定时间的第一条 select title,pubtime from article where pubtime SELECT strftime(%m/%d/%Y, 2004-10-31);10/31/2004Table 3.3 lists the conversions that can be performed by SQLite on a timestring.Table 3.3. Date and Time Conversion Specifiers StringMeaning%dDay of month, 01-31%fFractional seconds, SS.SSS%HHour, 00-23%jDay of year, 001-366%JJulian day number, DDDD.DDDD%mMonth, 00-12%MMinute, 00-59%sSeconds since 1970-01-01 (unix epoch)%SSeconds, 00-59%wDay of week, 0-6 (0 is Sunday)%WWeek of year, 01-53%YYear, YYYY% symbolDate and Time Modifiers Given one or more optional modifier arguments, strftime() can perform a calculation on the date given in timestring.To add or subtract a period of time, the days, hours, minutes, seconds, months and years modifiers can be used, as shown in these examples:sqlite SELECT strftime(%Y-%m-%d, 2004-10-31, +7 days);2004-11-07sqlite SELECT strftime(%H:%M, 22:00, +12 hours);10:00sqlite SELECT strftime(%Y-%m-%d %H:%M:%S,2004-01-01 00:00:00, -1 second, +1 year);2004-12-31 23:59:59NoteThe modifier keywords can be written as either singular or plural. In the last of the preceding examples, we used 1 second and 1 year rather than 1 seconds and 1 years for readability. SQLite does not understand English grammar, so either is always acceptable.In these examples we have used the same output format as the original timestring to return the date information in a format that can be recognized by SQLite. You should only format the date differently when you want to display it in your application in a particular way.To save having to enter the same format strings repeatedly when working with dates, SQLite provides four convenience functions that call strftime() with predefined formats.Use date() to return a date with the format string %Y-%m-%d and time() to return a time as %H:%S. The function datetime() returns the date and time using these two formats combined. Finally julianday() uses the %J format specifier to return the Julian day number.The arguments to all four functions are the same as strftime() except that the format argument is omitted. The following example uses datetime() to produce a more concise SQL statement:sqlite SELECT datetime(2004-01-01 00:00:00, -1 second, +1 year);2004-12-31 23:59:59Other modifiers allow you to adjust a date or time to the nearest significant value. Specifying start of month, start of year, or start of day will decrease the value given in timestring to midnight on the first of the month or year, or on that day respectively.When executed on any day during 2004, the start of year modifier returns 2004-01-01, as shown in the following example:sqlite SELECT datetime(now, start of year);2004-01-01 00:00:00Modifiers are applied to timestring in the order they appear in the statement, as shown in the following example. Note that had the second statement been executed on the last day of the month, the result would have been differentthe start of the following month would have been returned.sqlite SELECT datetime(now, start of month, +1 day);2004-07-02 00:00:00sqlite SELECT datetime(now, +1 day, start of month);2004-07-01 00:00:00Any number of modifiers can be combined, giving you considerable power when working with dates and times. For instance, the last day of the current month can be found using three modifiers in succession.sqlite SELECT date(now, +1 month, start of month, -1 day);2004-07-31Handling Different Time Zones The locale settings of your system will determine which time zone is used when displaying dates and times; however, the underlying system clock will use Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT)Greenwich Mean Time (GMT). Your time zone setting will specify a number of hours to be added to or subtracted from the UTC value to arrive at the correct local time.For instance, to find the local time in New York you have to subtract five hours from UTC, or four hours during daylight savings time. Even in Greenwich, the local time is UTC + 1 hour during the summer months.To convert between UTC and local time values when formatting a date, use the utc or localtime modifiers. The following examples were run on a system with the timezone set to Eastern Standard Time (UTC 5 hours).sqlite SELECT time(12:00, localtime);2000-01-01 07:00:00sqlite SELECT time(12:00, utc);2000-01-01 17:00:00SQLite Date And Time Functions /lang_datefunc.htmlSqlite3时间&函数Sqlite3支持的数据类型 日期函数 Sqlite3 函数2010年05月24日 星期一 10:50 Sqlite3支持的数据类型 NULLINTEGERREALTEXTBLOB但实际上,sqlite3也接受如下的数据类型:smallint 16 位元的整数。interger 32 位元的整数。decimal(p,s) p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值,s是指小数点後有几位数。如果没有特别指定,则系统会设为 p=5; s=0 。float 32位元的实数。double 64位元的实数。char(n) n 长度的字串,n不能超过 254。varchar(n) 长度不固定且其最大长度为 n 的字串,n不能超过 4000。graphic(n) 和 char(n) 一样,不过其单位是两个字元 double-bytes, n不能超过127。这个形态是为了支援两个字元长度的字体,例如中文字。vargraphic(n) 可变长度且其最大长度为 n 的双字元字串,n不能超过 2000date 包含了 年份、月份、日期。time 包含了 小时、分钟、秒。timestamp 包含了 年、月、日、时、分、秒、千分之一秒。SQLite包含了如下时间/日期函数:datetime().产生日期和时间date().产生日期time().产生时间strftime().对以上三个函数产生的日期和时间进行格式化datetime()的用法是:datetime(日期/时间,修正符,修正符.)date()和time()的语法与datetime()相同。在时间/日期函数里可以使用如下格式的字符串作为参数:YYYY-MM-DDYYYY-MM-DD HH:MMYYYY-MM-DD HH:MM:SSYYYY-MM-DD HH:MM:SS.SSSHH:MMHH:MM:SSHH:MM:SS.SSSnow其中now是产生现在的时间。举例(写这个笔记的时间是2006年10月17日晚8点到10点,测试环境:SQLite 2.8.17,WinXP,北京时间):例1.select datetime(now); 结果:2006-10-17 12:55:54例2.select datetime(2006-10-17); 结果:2006-10-17 12:00:00例3.select datetime(2006-10-17 00:20:00,+1 hour,-12 minute);结果:2006-10-17 01:08:00例4.select date(2006-10-17,+1 day,+1 year);结果:2007-10-18例5.select datetime(now,start of year);结果:2006-01-01 00:00:00例6.select datetime(now,start of month);结果:2006-10-01 00:00:00例7.select datetime(now,start of day);结果:2006-10-17 00:00:00例8.select datetime(now,+10 hour,start of day,+10 hour);结果:2006-10-17 10:00:00例9.select datetime(now,localtime);结果:2006-10-17 21:21:47例10.select datetime(now,+8 hour);结果:2006-10-17 21:24:45例3中的+1 hour和-12 minute表示可以在基本时间上(datetime函数的第一个参数)增加或减少一定时间。例5中的start of year表示一年开始的时间。从例8可以看出,尽管第2个参数加上了10个小时,但是却被第3个参数“start of day”把时间归零到00:00:00,随后的第4个参数在00:00:00的基础上把时间增加了10个小时变成了10:00:00。例9把格林威治时区转换成本地时区。例10把格林威治时区转换成东八区。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()的用法举例如下:例11.select strftime(%Y.%m.%d %H:%M:%S,now,localtime);结果:2006.10.17 21:41:09函数篇:算术函数 abs(X) 返回给定数字表达式的绝对值。 max(X,Y,.) 返回表达式的最大值。 min(X,Y,.) 返回表达式的最小值。 random(*) 返回随机数。 round(X,Y) 返回数字表达式并四舍五入为指定的长度或精度。 字符处理函数 length(X) 返回给定字符串表达式的字符个数。 lower(X) 将大写字符数据转换为小写字符数据后返回字符表达式。 upper(X) 返回将小写字符数据转换为大写的字符表达式。 substr(X,Y,Z) 返回表达式的一部分。 randstr() quote(A) like(A,B) 确定给定的字符串是否与指定的模式匹配。 glob(A,B) 条件判断函数 coalesce(X,Y,.) ifnull(X,Y) nullif(X,Y) 集合函数 avg(X) 返回组中值的平均值。 count(X) 返回组中项目的数量。 max(X) 返回组中值的最大值。 min(X) 返回组中值的最小值。 sum(X) 返回表达式中所有值的和。 其他函数 typeof(X) 返回数据的类型。 last_insert_rowid() 返回最后插入的数据的ID。 sqlite_version(*) 返回SQLite的版本。 change_count() 返回受上一语句影响的行数。 last_statement_change_count()管理工具:/ SQLite :/ QT之文件操作1: 定向打开文件,并读取将要打开的文件导入到你的工程中,具体如何导入,在QT帮助文件welcom界面,点击GetingStarted 再选则 Creating a QT C+ Application 就会有一个实例。QString fileName ;fileName.prepend(:/)/在fileName前加字符 :/此字符是缺省的相对路径不可少,如果你导入的文件不止一个, 他们放在一个名为files的文件夹内,那么,你就这样设置文件前缀fileName.prepend( :/files/),了解了吧。fileName.append(.txt);/在fileName后加字符总之在设置文件名时,很灵活,可以使用通过信号槽传来的信息数据,也可以自己设制。如 fielName = HelloQFile inPutFile(fileName);/最终inPutFile所绑定的打开一个文件的相对路径字符串。例如:fileName = :/files/first.txt 接下来就是打开文件 inPutFile.open(QIODevice:ReadOnly);/设为只读形式打开QTextStream in(&inPutFile);/用QTextStream 类对象来读取,因为可以用其中的许多函数QString line = in.readLine();/一行行的读QString line = in.readAll();/一下子都读完Qt之文件操作 分类: Meego2010-12-19 02:141690人阅读评论(0)收藏举报今天周末,早上起来朋友买了电脑,昨晚特意下了个上网本的meego 1.1,早上起来装了下,界面还行,不过我找了半天都没不知到怎么关机。还好找到了终端,输入shutdown命令关了电脑,后来查了下才发现meego是直接按power键关机的。今天学习QT的文件操作1、QIODevice直接继承自QObjectQIODevice类是输入/输出设备的基类。QIODevice为设备提供了公共实现和抽象接口用于读写块数据。QIODevice是一个抽象类,不能被实例化。被Q3Socket,Q3SocketDevice,QAbstractSocket,QBuffer,QFile,QLocalSocket,QNetworkReply,QProcess继承.=2、QFile继承自QIODeviceQFile类是一个操作文件的输入/输出设备。QFile是用来读写二进制文件和文本文件的输入/输出设备。QFile可以自己单独被使用,但是如果和QDataStream或QTextStream一起使用将更加方便。文件名通常可以通过构造函数来传递,但也可以使用setName()来设置。目录分隔符在任何操作系统下都使用“/,“/不被支持。你可以通过exists()来检查一个文件是否存在并且可以通过remove()来移去一个文件。更多操作系统相关的高级文件系统操作QT提供了QFileInfo和QDir类.文件可以用open()来打开、用close()来关闭、用flush()来刷新。数据通常可以使用QDataStream或者QTextStream进行读写,但你也可以使用read(),readLine(),readAll(),write()读写。QFile也支持getChar(),putChar(),和ungetChar()size()可以返回文件的大小。你可以通过使用pos()函数得到当前文件位置或者使用seek()移到一个新的文件位置。如果你到了文件的末尾,atEnd()返回真。例1:一行一行读取文件c-sharp view plaincopyprint?1. #include 2. #include 3. #include 4. #include 5. int main(int argc, char *argv) 6. 7. QCoreApplication a(argc, argv); 8. /中文支持 9. QTextCodec *codec = QTextCodec:codecForName(UTF-8); 10. QTextCodec:setCodecForCStrings(codec); 11.12. QFile file(/home/administrator/testdir/test.txt); 13. if(!file.open(QIODevice:ReadOnly | QIODevice:Text) 14. qDebug()Cant open the file!endl; 15. 16. while(!file.atEnd() 17. QByteArray line = file.readLine(); 18. QString str(line); 19. qDebug() str; 20. 21. return a.exec(); 22. 使用QTextStream读取文件cpp view plaincopyprint?1. #include 2. #include 3. #include 4. #include 5. int main(int argc, char *argv) 6. 7. QCoreApplication a(argc, argv); 8. /中文支持 9. QTextCodec *codec = QTextCodec:codecForName(UTF-8); 10. QTextCodec:setCodecForCStrings(codec); 11. /QTextCodec:setCodecForTr(codec); 12. /QTextCodec:setCodecForLocale(codec); 13. QFile file(/home/administrator/testdir/test.txt); 14. if(!file.open(QIODevice:ReadOnly | QIODevice:Text) 15. qDebug()Cant open the file!endl; 16. 17. QTextStream in(&file); 18. while( !in.atEnd() 19. QString line = in.readLine(); 20. qDebug() line; 21. 22. return a.exec(); 23. QDataStream 重载了运算符了读数据cpp view plaincopyprint?1. #include 2. #include 3. #include 4. #include 5. int main(int argc, char *argv) 6. 7. QCoreApplication a(argc, argv); 8. /中文支持 9. QTextCodec *codec = QTextCodec:codecForName(UTF-8); 10. QTextCodec:setCodecForCStrings(codec); 11. /QTextCodec:setCodecForTr(codec); 12. /QTextCodec:setCodecForLocale(codec); 13. QFile file(/home/administrator/testdir/test.txt); 14. if(!file.open(QIODevice:ReadWrite | QIODevice:Text) 15. qDebug()Cant open th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论