热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

sqlite数据库默认时间值'now'-sqlitedatabasedefaulttimevalue'now'

IsitpossibleinasqlitedatabasetocraeteatablethathasatimestampcolumnthatdefaulttoDA

Is it possible in a sqlite database to craete a table that has a timestamp column that default to DATETIME('now') ?

是否可以在sqlite数据库中获取具有默认为DATETIME('now')的时间戳列的表?

Like this:

是这样的:

CREATE TABLE test (
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    t TIMESTAMP DEFAULT DATETIME('now')
);

This gives an error... How to resolve?

这给了一个错误……如何解决?

6 个解决方案

#1


232  

i believe you can use

我相信你可以用

CREATE TABLE test (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  t TIMESTAMP
  DEFAULT CURRENT_TIMESTAMP
);

as of version 3.1 (source)

截至3.1版本(来源)

#2


76  

according to dr. hipp in a recent list post:

hipp博士在最近的一篇文章中写道:

CREATE TABLE whatever(
     ....
     timestamp DATE DEFAULT (datetime('now','localtime')),
     ...
);

#3


31  

It's just a syntax error, you need parenthesis: (DATETIME('now'))

它只是一个语法错误,您需要括号:(DATETIME('now'))

If you look at the documentation, you'll note the parenthesis that is added around the 'expr' option in the syntax.

如果查看文档,您将注意到语法中围绕“expr”选项添加的括号。

#4


10  

This is a full example based on the other answers and comments to the question. In the example the timestamp (created_at-column) is saved as unix epoch UTC timezone and converted to local timezone only when necessary.

这是一个完整的例子,基于其他答案和对问题的评论。在本例中,时间戳(created_at-column)被保存为unix纪元UTC时区,并仅在必要时转换为本地时区。

Using unix epoch saves storage space - 4 bytes integer vs. 24 bytes string when stored as ISO8601 string, see datatypes. If 4 bytes is not enough that can be increased to 6 or 8 bytes.

使用unix epoch保存存储空间- 4字节整数和24字节字符串存储为ISO8601字符串时,请参见数据类型。如果4字节不够,可以增加到6或8字节。

Saving timestamp on UTC timezone makes it convenient to show a reasonable value on multiple timezones.

在UTC时区上保存时间戳可以方便地在多个时区显示合理的值。

SQLite version is 3.8.6 that ships with Ubuntu LTS 14.04.

SQLite版本是3.8.6,附带了Ubuntu LTS 14.04。

$ sqlite3 so.db
SQLite version 3.8.6 2014-08-15 11:46:33
Enter ".help" for usage hints.
sqlite> .headers on

create table if not exists example (
   id integer primary key autoincrement
  ,data text not null unique
  ,created_at integer(4) not null default (strftime('%s','now'))
);

insert into example(data) values
 ('foo')
,('bar')
;

select
 id
,data
,created_at as epoch
,datetime(created_at, 'unixepoch') as utc
,datetime(created_at, 'unixepoch', 'localtime') as localtime
from example
order by id
;

id|data|epoch     |utc                |localtime
1 |foo |1412097842|2014-09-30 17:24:02|2014-09-30 20:24:02
2 |bar |1412097842|2014-09-30 17:24:02|2014-09-30 20:24:02

Localtime is correct as I'm located at UTC+2 DST at the moment of the query.

Localtime是正确的,因为我在查询时位于UTC+2 DST。

#5


3  

It is syntax error because you did not write parenthesis

这是语法错误,因为你没有写括号

if you write

如果你写

Select datetime('now') then it will give you utc time but if you this write it query then you must add parenthesis before this so (datetime('now')) for UTC Time. for local time same Select datetime('now','localtime') for query

选择datetime('now')然后它会给你utc时间,但如果你写它查询,那么你必须在此之前添加括号(datetime('now'))。对于本地时间相同的选择datetime('now','localtime')用于查询。

(datetime('now','localtime'))

(datetime(“现在”、“作用”))

#6


3  

It may be better to use REAL type, to save storage space.

最好是使用真实的类型,以节省存储空间。

Quote from 1.2 section of Datatypes In SQLite Version 3

引用SQLite版本3中的1.2节数据类型

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values

SQLite没有存储日期和/或时间的存储类。相反,SQLite的内置日期和时间函数可以将日期和时间存储为文本、实值或整数值

CREATE TABLE test (
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    t REAL DEFAULT (datetime('now', 'localtime'))
);

see column-constraint .

看到column-constraint。

And insert a row without providing any value.

插入一行而不提供任何值。

INSERT INTO "test" DEFAULT VALUES;

推荐阅读
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
  • 本文介绍了使用C++Builder实现获取USB优盘序列号的方法,包括相关的代码和说明。通过该方法,可以获取指定盘符的USB优盘序列号,并将其存放在缓冲中。该方法可以在Windows系统中有效地获取USB优盘序列号,并且适用于C++Builder开发环境。 ... [详细]
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
  • 知识图谱表示概念:知识图谱是由一些相互连接的实体和他们的属性构成的。换句话说,知识图谱是由一条条知识组成,每条知识表示为一个SPO三元组(Subject-Predicate-Obj ... [详细]
  • 获取时间的函数js代码,js获取时区代码
    本文目录一览:1、js获取服务器时间(动态)2 ... [详细]
  • 国庆节到了,安利一个Android的自动动态授权插件
    Android的老铁都知道申请权限时,除了要在AndroidManifest添加权限,还需要在activity中通过requestpermission对 ... [详细]
  • 介绍平常在多线程开发中,总避免不了线程同步。本篇就对net多线程中的锁系统做个简单描述。目录一:lock、Monitor1:基础 ... [详细]
  • Abp+MongoDb改造默认的审计日志存储位置
    一、背景在实际项目的开发当中,使用AbpZero自带的审计日志功能写入效率比较低。其次审计日志数据量中后期十分庞大,不适合与业务数据存放在一起。所以我们可以重新实现A ... [详细]
  • unitUnit1;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,Syst ... [详细]
  • 4.3.2Tuple是否可以跨页面PostgreSQLusesafixedpagesize(commonly8kB),anddoesnotallowtuplestospanmult ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了PHP常量的定义和使用方法,包括常量的命名规则、大小写敏感性、全局范围和标量数据的限制。同时还提到了应尽量避免定义resource常量,并给出了使用define()函数定义常量的示例。 ... [详细]
  • 本文分析了Wince程序内存和存储内存的分布及作用。Wince内存包括系统内存、对象存储和程序内存,其中系统内存占用了一部分SDRAM,而剩下的30M为程序内存和存储内存。对象存储是嵌入式wince操作系统中的一个新概念,常用于消费电子设备中。此外,文章还介绍了主电源和后备电池在操作系统中的作用。 ... [详细]
  •   《WindowsAzurePlatform系列文章目录》  本文将介绍如何在AzureSQLDatabase创建只读用户。  请先按照笔者之前的文章:AzureSQLDatabas ... [详细]
author-avatar
Jump_jiedB0_666
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有