简体   繁体   English

删除超过x天的android SQLite行

[英]deleting android SQLite rows older than x days

I want to delete all rows in table MYTABLE which are older than x days. 我想删除表MYTABLE中超过x天的所有行。 Column SAVE_DATE Long is the time when the row was inserted in table. 列SAVE_DATE长是在表中插入行的时间。

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? 怎么了?

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);

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. 您不需要用于访问sqlite DB的主程序中的时间/日期函数,而是直接使用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. 在这里我使用了'datetime',因为这也可以让你以后搜索小时/分钟/秒。 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 '. 您可以在上面的网站“ Modifiers ”一节中看到更多这些可能性。

删除时间戳或日期字段存储时间超过2天的数据(以毫秒为单位)或一个纪元整数。

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

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: 使用最新版本的ORMLite for SQLite Android: http ://ormlite.com/sqlite_java_android_orm.shtml,您可以使用以下代码实现此目的:

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

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

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