简体   繁体   English

如何在SQLite Android中删除所有数据历史表的时间早于30天

[英]How to delete all data HistoryTable have time older than 30days in SQLite Android

CREATE TABLE 创建表

public static void createTable(SQLiteDatabase db){  
String sql = "CREATE TABLE " + TABLE_HISTORY + " ("  
                    + COL_ID + " INTEGER PRIMARY KEY NOT NULL, "  
                    + COL_TITLE + " TEXT, "  
                    + COL_MEDIA_NAME + " TEXT, "  
                    + COL_IMAGE_URL + " TEXT, "  
                    + COL_URL + " TEXT, "  
                    + COL_CREATED_AT + " INTEGER NOT NULL DEFAULT 0, "  
                    + COL_UPDATED_AT + " INTEGER NOT NULL DEFAULT 0 "  
                    + ")";  
    db.execSQL(sql);  
}

INSERT TABLE 插入表

 public static long insertTableHistory(DatabaseDAO db, int mId, String title, String mediaName, String imageUrl, String url) {
    SQLiteDatabase database = db.getWritableDatabase();
    String sql =
            "INSERT INTO " + TABLE_HISTORY + " ("
                    + COL_ID + ", "
                    + COL_TITLE + ", "
                    + COL_MEDIA_NAME + ", "
                    + COL_IMAGE_URL + ", "
                    + COL_URL + ", "
                    + COL_CREATED_AT + ","
                    + COL_UPDATED_AT +
                    ")"
                    + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
SQLiteStatement statement = database.compileStatement(sql);
    long id = -1;
    try {
        database.beginTransaction();
        statement.clearBindings();
        statement.bindLong(1, mId);
        statement.bindString(2, title);
        statement.bindString(3, mediaName);
        statement.bindString(4, imageUrl);
        statement.bindString(5, url);
        statement.bindLong(6, System.currentTimeMillis());
        statement.bindLong(7, System.currentTimeMillis());
        id = statement.executeInsert();
        database.setTransactionSuccessful();

    } catch (Exception e) {
        database.endTransaction();
    } finally {
        database.endTransaction();
    }
    return id;
}

I try it But it not working. 我尝试了,但是没有用。

public static void deleteDataOlderThan30Days(DatabaseDAO databaseDAO) {

    String sql = "DELETE FROM " + TABLE_HISTORY + " WHERE " + COL_CREATED_AT + "<= date('now','-30 day')";
    SQLiteDatabase database = databaseDAO.getWritableDatabase();
    database.execSQL(sql);
}

How to I can delete all data at HistotyTable older than 30days? 如何删除30天以上的HistotyTable上的所有数据?
Please. 请。 Help me! 帮我!

Your timestamp columns contain a number, which is the number of milliseconds since 1970. The date function returns a string in the format yyyy-mm-dd . 您的时间戳列包含一个数字,它是自1970年以来的毫秒数date函数返回格式的字符串yyyy-mm-dd

Either change your database to store the timestamps in the same format, or change the query to compute the date limit in the millisecond format: 更改数据库以相同格式存储时间戳,或者更改查询以毫秒格式计算日期限制:

... WHERE CreatedAt <= strftime('%s', 'now', '-30 days') * 1000

Used datediff function 使用的datediff函数

When it comes to SQL, you have to specify what you mean by "older than a day". 对于SQL,必须指定“早于一天”的含义。

DATEDIFF: it uses day boundary midnight so run it at 19th October 00:05 and you'll delete rows 6 minutes old (18th October 23:59) DATEDIFF:它使用白天边界午夜,因此在10月19日00:05运行它,您将删除6分钟前的行(10月18日23:59)

24 hours? 24小时?

Yesterday midnight? 昨天午夜? Run code on 19th October, delete rows before 18th? 在10月19日执行代码,在18日之前删除行?

Also, don't put a function on a column. 另外,不要将函数放在列上。

This assumes 24 hours to the minute: 假设分钟为24小时:

DELETE MyTableWhere WHERE MyColumn < DATEADD(day, -1, GETDATE()) This assumes yesterday midnight: DELETE MyTableWhere WHERE MyColumn <DATEADD(day,-1,GETDATE())假定昨天午夜:

DELETE MyTableWhere WHERE MyColumn < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1) 删除MyTable所在的位置MyColumn <DATEADD(DAY,DATEDIFF(DAY,0,GETDATE()),-1)

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

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