删除超过x天的android SQLite行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11771580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:48:40  来源:igfitidea点击:

deleting android SQLite rows older than x days

androidsqlite

提问by George D

I want to delete all rows in table MYTABLE which are older than x days. Column SAVE_DATE Long is the time when the row was inserted in table.

我想删除表 MYTABLE 中所有早于 x 天的行。列 SAVE_DATE Long 是该行插入表中的时间。

I tried this but apparently it deletes all my rows:

我试过这个,但显然它删除了我所有的行:

long daysInMiliSec = new Date().getTime() - X
            * (24L * 60L * 60L * 1000L);
return db.delete(MYTABLE , SAVE_DATE
            " <= ?", new String[] { "" + daysInMiliSec }

What is wrong?

怎么了?

回答by Yaqub Ahmad

Below query will delete data older than 2 days:

以下查询将删除超过 2 天的数据:

String sql = "DELETE FROM myTable WHERE Save_Date <= date('now','-2 day')"; 
db.execSQL(sql);

回答by user136036

Since it's the first hit on google some more explanation for beginners:
You do not need the time/date functions from the main program you use to access the sqlite DB but use the sqlite date functions directly.

由于它是 google 上的第一次命中,因此对初学者有更多解释:
您不需要用于访问 sqlite 数据库的主程序中的时间/日期函数,而是直接使用 sqlite 日期函数。

You create the table with the row entry for the age with for example:

您可以使用年龄的行条目创建表,例如:

CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, text TEXT, age INTEGER)

You write to it with

你写信给它

INSERT INTO test (text, age) VALUES ("bla", datetime('now'))

Here I used 'datetime' because this also will let you later search for hours/minutes/seconds. If you don't need that 'date('now')' is enough.

在这里我使用了“日期时间”,因为这也可以让您稍后搜索小时/分钟/秒。如果您不需要 'date('now')' 就足够了。

Here is an explanation for the date function: https://www.sqlite.org/lang_datefunc.html

这里是日期函数的解释:https: //www.sqlite.org/lang_datefunc.html

To select everything older than for example 5 minutes:

要选择早于 5 分钟的所有内容:

SELECT * FROM test WHERE age <= datetime('now', '-5 minutes')

You can see more of those possibilities on the website above under the paragraph 'Modifiers'.

您可以在上面网站的“修饰符”段落下看到更多这些可能性。

回答by Brian Feldman

Delete data older than 2 days when the timestamp or date field is stored in milliseconds or an epoch integer.

当时间戳或日期字段以毫秒或纪元整数存储时,删除超过 2 天的数据。

DELETE FROM update_log WHERE timestamp <= strftime('%s', datetime('now', '-2 day'));

回答by Simon

With the latest version of ORMLite for SQLite Android: http://ormlite.com/sqlite_java_android_orm.shtml, you may achieve this by using the following code:

使用适用于 SQLite Android 的最新版 ORMLite:http://ormlite.com/sqlite_java_android_orm.shtml ,您可以使用以下代码实现此目的:

                        String sql = "DELETE FROM graph WHERE time <= 1522405117";
                        dbHelper.getWritableDatabase().execSQL(sql);