CREATE TABLE如何刪除所有數據HistoryTable時間比30天年長SQLite中的Android
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);
}
插入表格
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;
}
我嘗試但它n不工作。
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天以上的所有數據?
請。幫我!
在我的例子。我該怎麼辦 ?謝謝 –
這在SQLite中不起作用。 –