码迷,mamicode.com
首页 > 数据库 > 详细

Sqlite实现默认时间为当前时间列的方法(转)

时间:2014-09-29 11:48:07      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   strong   

原文地址: http://blog.csdn.net/derryzhang/article/details/5033209

 

SQL Server中,创建表格的时候,对于时间列有时候我们可以根据需要指定默认值为当前时间(也就是说记录生成的时候有默认的时间戳)。例如:

   

[xhtml] view plaincopy
  1. create table log(  
  2.   
  3.    content varchar(256),  
  4.   
  5.    logtime datetime default getdate()  
  6.   
  7.    )  

  

 

    然而在Sqlite中如何实现呢?查文档得知Sqlite中并没有getdate()函数,但其系统内置函数有datetime(),因此能不能按照下面的写法实现默认时间戳呢:

    

[xhtml] view plaincopy
  1. create table log(  
  2.   
  3.     content varchar(256),  
  4.   
  5.     logtime datetime default datetime(‘now‘)  
  6.   
  7.     )  

  

 

    答案是否定的,会提示语法错误。那么应该如何声明呢?如下所示:

   

[c-sharp] view plaincopy
  1. create table log(  
  2.   
  3.    content varchar(256),  
  4.   
  5.    logtime TIMESTAMP default CURRENT_TIMESTAMP  
  6.   
  7.    )  

    

 

    这个可以达到效果,但是默认的时间是以格林尼治标准时间为基准的,因此在中国使用的话会正好早8个小时。为了解决这个问题,我们可以这样声明:

   

[xhtml] view plaincopy
  1. create table log(  
  2.   
  3.     content varchar(256),  
  4.   
  5.     logtime TIMESTAMP default (datetime(‘now‘, ‘localtime‘))  
  6.   
  7.     )  

 

 

    测试一下,一切正常:)

Sqlite实现默认时间为当前时间列的方法(转)

标签:style   blog   http   color   io   os   使用   ar   strong   

原文地址:http://www.cnblogs.com/SharkBin/p/3999491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!