简体   繁体   English

如何根据时间戳删除mysql数据?

[英]How do I delete mysql data based on timestamp?

I am using System.Data.SqlLite.dll lite version 1.0.105.0 我正在使用System.Data.SqlLite.dll lite版本1.0.105.0

I have a very simple MySQL database. 我有一个非常简单的MySQL数据库。

I create a single table in the Database using the following: 我使用以下方法在数据库中创建一个表:

CREATE TABLE SessionInfo (session nvarchar(100), state nvarchar(25), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP)

I (successfully) add data to the table: 我(成功)将数据添加到表中:

INSERT INTO SessionInfo (session, state) VALUES ('test', '1')

When I try to purge data from the table using this statement: 当我尝试使用此语句从表中清除数据时:

DELETE FROM SessionInfo WHERE ts < ADDDATE( NOW(), INTERVAL -1 MINUTE )

(I have also tried the following) (我也尝试了以下方法)

DELETE FROM SessionInfo WHERE ts < CURRENT_TIMESTAMP - interval '1' minute
DELETE FROM SessionInfo WHERE ts < DATE_SUB(NOW(), INTERVAL 1 MINUTE)

In all cases, I get this error: 在所有情况下,我都会收到此错误:

System.Data.SQLite.SQLiteException was unhandled
ErrorCode=1
HResult=-2147467259
Message=SQL logic error or missing database
near "MINUTE": syntax error
Source=System.Data.SQLite

When I try either of these: 当我尝试以下任一方法时:

DELETE FROM SessionInfo WHERE ts < NOW() - INTERVAL 1 MINUTE
DELETE FROM SessionInfo WHERE ts < NOW() - INTERVAL '1' MINUTE

I get this error: 我收到此错误:

System.Data.SQLite.SQLiteException was unhandled
ErrorCode=1
HResult=-2147467259
Message=SQL logic error or missing database
near "'1'": syntax error
Source=System.Data.SQLite

I tried wrapping timestamp in UNIX_TIMESTAMP macro... 我尝试将时间戳包装在UNIX_TIMESTAMP宏中...

DELETE FROM SessionInfo WHERE UNIX_TIMESTAMP(ts) < 
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE))

Still same error: 还是一样的错误:

System.Data.SQLite.SQLiteException was unhandled
ErrorCode=1
HResult=-2147467259
Message=SQL logic error or missing database
near "1": syntax error
Source=System.Data.SQLite

I tried deleting records in between timestamps: 我尝试在时间戳之间删除记录:

DELETE FROM SessionInfo WHERE ts between now() - interval 1 hour and now()

I still get the same result: 我仍然得到相同的结果:

System.Data.SQLite.SQLiteException was unhandled
ErrorCode=1
HResult=-2147467259
Message=SQL logic error or missing database
near "1": syntax error

So, I just tried this: 所以,我只是试了这个:

DELETE FROM SessionInfo WHERE ts < CURRENT_TIMESTAMP

and ALL of my records are deleted... so I know I can delete... just not the stuff I want to delete 并且我的所有记录都被删除了...所以我知道我可以删除...而不是我要删除的内容

Thanks to LSerni, here is my working code: ( note: I was using minutes for testing... but real code will be in days ) 感谢LSerni,这是我的工作代码:(注意:我花了几分钟来进行测试...但是真正的代码将在几天内完成)

DELETE FROM SessionInfo WHERE ts < DATETIME('NOW', '-7 DAY')

You are using MySQL syntax, but SQLite does not support it. 您正在使用MySQL语法,但是SQLite不支持它。 There is no NOW() function, nor there is a date adding function. 没有NOW()函数,也没有日期添加函数。 You must use date modifiers . 您必须使用日期修饰符

Try: 尝试:

 SELECT DATETIME('NOW', '-1 MINUTE');

or 要么

 DELETE ... WHERE ts < DATETIME('NOW', '-1 MINUTE');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM