我试图以这种方式实现触发器onCallBack(),但是它既不会抛出错误,也不会起作用。
private static Callback sRoomDatabaseCallback = new Callback(){
@Override
public void onOpen (@NonNull SupportSQLiteDatabase db){
super.onOpen(db);
// If you want to keep the data through app restarts,
// new PopulateDbAsync(INSTANCE).execute();
// You can run trigger for database house keeping jobs directly here
db.execSQL("Create Trigger IF NOT EXISTS "+DbConfig.Trigr_NewsAnnouncementDelete +
" After Insert On " + DbConfig.Tbl_NewsAnnouncement+
" Begin Delete From "+DbConfig.Tbl_NewsAnnouncement +
" WHERE DATE(CreatedDate) > DATE('now','-10 days');" +
" END ");
}
};
在调试时,我发现NewsAnnouncementDatabase_Impl.java(自动创建的文件)具有以下代码行
@SuppressWarnings("unchecked")
public class NewsAnnouncementDatabase_Impl extends NewsAnnouncementDatabase
{
private volatile NewsAnnouncementDao _newsAnnouncementDao;
private volatile DatabaseHouseKeepingDao _databaseHouseKeepingDao;
@Override
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration configuration) {
final SupportSQLiteOpenHelper.Callback _openCallback = new RoomOpenHelper(configuration, new RoomOpenHelper.Delegate(1) {
@Override
public void createAllTables(SupportSQLiteDatabase _db) {
_db.execSQL("CREATE TABLE IF NOT EXISTS `NewsAnnouncement` (`Id` TEXT NOT NULL, `Title` TEXT, `Image` TEXT, `Description` TEXT, `PublishDate` TEXT, `CreatedDate` TEXT, `UserName` TEXT, `ShortDescription` TEXT, PRIMARY KEY(`Id`))");
.....
...
}
有人可以帮我找出触发句法或任何其他想法中的问题吗?我正在尝试使用触发器从数据库中删除最近10天的记录。
谢谢。
答案 0 :(得分:1)
触发器语法看起来不错并且可以正常工作(请参阅下文),尽管删除创建日期大于10天的所有行的逻辑可能会导致删除插入的行,除非该行的创建日期是10天或更多。
要使触发器起作用,存储在CreatedDate
列中的值必须采用可接受的日期格式,即yyyy-mm-yy(例如2018-09-11)。
请考虑以下内容(带有数据的SQL测试版本):-
DROP TABLE IF EXISTS NewsAnnouncement;
DROP TRIGGER IF EXISTS Trigr_NewsAnnouncementDelete;
CREATE TABLE IF NOT EXISTS `NewsAnnouncement` (`Id` TEXT NOT NULL, `Title` TEXT, `Image` TEXT, `Description` TEXT, `PublishDate` TEXT, `CreatedDate` TEXT, `UserName` TEXT, `ShortDescription` TEXT, PRIMARY KEY(`Id`));
SELECT * FROM NewsAnnouncement;
Create Trigger IF NOT EXISTS Trigr_NewsAnnouncementDelete
AFTER Insert On NewsAnnouncement
Begin Delete From NewsAnnouncement
WHERE DATE(CreatedDate) > DATE('now','-10 days');
END
;
SELECT * FROM sqlite_master WHERE type = 'trigger';
INSERT INTO NewsAnnouncement VALUES
('test001','title001','image001','desc001','2018-01-01','2018-01-01','fred','shordesc001'),
('test002','title002','image002','desc002','2018-10-01','2018-10-01','fred','shordesc002'),
('test003','title003','image003','desc003','2018-12-01','2018-12-01','fred','shordesc003'),
('test004','title004','image004','desc004','2018-08-01','2018-08-01','fred','shordesc004'),
('test005','title005','image005','desc005','2018-12-01','2018-12-01','fred','shordesc005'),
('test006','title006','image006','desc006','2018/08/01','2018/08/01','fred','shordesc006'),
('test007','title007','image007','desc007','2018/12/01','2018/12/01','fred','shordesc007')
;
SELECT *,date(CreatedDate) FROM NewsAnnouncement;
结果将是:-
那是:-
可以使用SELECT * FROM sqlite_master WHERE type = 'trigger';
来查看触发器是否存在。输出将遵循以下行(仅突出显示的行):-