如何删除 SQLite 中超过 30 天的所有数据历史表 Android
How to delete all data HistoryTable have time older than 30days in SQLite Android
创建 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);
}
插入 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;
}
我试了一下但是不行
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);
}
如何删除 HistotyTable 中超过 30 天的所有数据?
请。帮帮我!
使用了datediff函数
说到SQL,你要具体说明"older than a day"是什么意思。
DATEDIFF:它使用日边界午夜,所以 运行 它在 10 月 19 日 00:05 并且您将删除 6 分钟前的行(10 月 18 日 23:59)
24 小时?
昨天半夜? 运行 10 月 19 日的代码,是否删除 18 日之前的行?
此外,不要将函数放在列上。
这假设 24 小时到分钟:
删除
MyTableWhere
在哪里
MyColumn < DATEADD(天,-1,GETDATE())
假设昨天午夜:
删除
MyTableWhere
在哪里
MyColumn < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1)
您的时间戳列包含一个数字,这是自 1970 年以来的毫秒数。
date
函数 returns 格式为 yyyy-mm-dd
.
的字符串
要么更改数据库以相同格式存储时间戳,要么更改查询以毫秒格式计算日期限制:
... WHERE CreatedAt <= strftime('%s', 'now', '-30 days') * 1000
创建 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);
}
插入 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;
}
我试了一下但是不行
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);
}
如何删除 HistotyTable 中超过 30 天的所有数据?
请。帮帮我!
使用了datediff函数
说到SQL,你要具体说明"older than a day"是什么意思。
DATEDIFF:它使用日边界午夜,所以 运行 它在 10 月 19 日 00:05 并且您将删除 6 分钟前的行(10 月 18 日 23:59)
24 小时?
昨天半夜? 运行 10 月 19 日的代码,是否删除 18 日之前的行?
此外,不要将函数放在列上。
这假设 24 小时到分钟:
删除 MyTableWhere 在哪里 MyColumn < DATEADD(天,-1,GETDATE()) 假设昨天午夜:
删除 MyTableWhere 在哪里 MyColumn < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1)
您的时间戳列包含一个数字,这是自 1970 年以来的毫秒数。
date
函数 returns 格式为 yyyy-mm-dd
.
要么更改数据库以相同格式存储时间戳,要么更改查询以毫秒格式计算日期限制:
... WHERE CreatedAt <= strftime('%s', 'now', '-30 days') * 1000