在使用SQLite更新current

编程入门 行业动态 更新时间:2024-10-21 17:34:07
本文介绍了在使用SQLite更新current_timestamp时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

每行更新时,我都希望使用当前时间戳更新字段。

I want to update a field with the current timestamp whenever the row is updated.

在MySQL中,我会在声明表时这样做

In MySQL I would do, when declaring the table

LastUpdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP

但是更新时部分不适用于SQLite。 我找不到自动执行此操作的方法,需要声明触发器吗?

But the "on update" part does not work with SQLite. I could not find a way to do it automatically, do I need to declare a trigger?

编辑:进行记录,这是我当前的触发器:

EDIT: For the record, here is my current trigger:

CREATE TRIGGER [UpdateLastTime] AFTER UPDATE ON Package FOR EACH ROW BEGIN UPDATE Package SET LastUpdate = CURRENT_TIMESTAMP WHERE ActionId = old.ActionId; END

谢谢

推荐答案

是的,您需要使用触发器。 (只需检查:您发布的触发器是否正常工作?乍看之下对我来说还不错。)

Yes, you'd need to use a trigger. (Just checking: is your posted trigger working correctly? At first glance, it looks fine to me.)

MySQL的 ON UPDATE CURRENT_TIMESTAMP 是一个非常独特的单用途快捷方式。就是这样;此构造 不能类似地用于 TIMESTAMP 以外的任何其他值或任何列类型。 (请注意如何在 TIMESTAMP 键入页面,而不是 创建表 code>页面,因为此功能特定于 TIMESTAMP 列而不是 CREATE TABLE 语句通常。)还值得一提的是,虽然它特定于 TIMESTAMP 类型,但 SQLite甚至没有不同的日期/时间类型。

MySQL's ON UPDATE CURRENT_TIMESTAMP is a pretty unique, single-purpose shortcut. It is what it is; this construct cannot be used similarly for any other values or for any column types other than TIMESTAMP. (Note how this functionality is defined on the TIMESTAMP type page instead of the CREATE TABLE page, as this functionality is specific to TIMESTAMP columns and not CREATE TABLE statements in general.) It's also worth mentioning that while it's specific to a TIMESTAMP type, SQLite doesn't even have distinct date/time types.

据我所知,没有其他RDBMS提供此快捷方式来代替使用实际触发器。根据我的阅读,必须使用触发器在MS SQL,SQLite,PostgreSQL和Oracle上完成此操作。

As far as I know, no other RDBMS offers this shortcut in lieu of using an actual trigger. From what I've read, triggers must be used to accomplish this on MS SQL, SQLite, PostgreSQL, and Oracle.

给路人的最后一封信:

这不要与 ON UPDATE 与外键约束有关的子句。那是完全不同的,可能所有支持外键约束的RDBMS都具有(包括MySQL和SQLite)。

This is not to be confused with ON UPDATE clauses in relation to foreign key constraints. That's something entirely different, which likely all RDBMSs that support foreign key constraints have (including both MySQL and SQLite).

更多推荐

在使用SQLite更新current

本文发布于:2023-10-24 19:51:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQLite   current

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!