我正在尝试创建一个包含2个事件的表bus
。一个AutoDelete
用于维护总线表中的数据(这个工作),另一个用于删除表中没有记录的表。 AutoDelete
我不知道如何在事件SELECT COUNT(*) FROM bus
中将条件设置为DropBusTable
以使其生效,因为我没有时间作为另一个我有条件的时间。
我感谢任何帮助。
stt.execute("CREATE TABLE IF NOT EXISTS bus"
+ "(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
+ "mac VARCHAR(30) NOT NULL UNIQUE,"
+ "route int(11) NOT NULL,"
+ "latitude FLOAT(10,6) NOT NULL,"
+ "longitude FLOAT(10,6) NOT NULL,"
+ "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
stt.execute("CREATE EVENT IF NOT EXISTS AutoDelete "
+ "ON SCHEDULE EVERY 3 MINUTE "
+ "DO "
+ "DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE)");
// I tried this statement here but it does not work.
stt.execute("CREATE EVENT IF NOT EXISTS DropBusTable "
+ "Do "
+ "DROP TABLE IF EXISTS bus");
答案 0 :(得分:0)
您可以使用以下方法重置计数器:
ALTER TABLE <table_name> AUTO_INCREMENT = 1
将其添加到现有任务中:
stt.execute("CREATE EVENT IF NOT EXISTS AutoDelete "
+ "ON SCHEDULE EVERY 3 MINUTE "
+ "DO BEGIN"
+ "DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE);"
+ "ALTER TABLE bus AUTO_INCREMENT = 1 ;"
+ "END;");
有关详情,请参阅 article 。