简体   繁体   中英

Creating event to delete records in the table

I am trying to get my mysql event to work but I am facing problem that the bus table is being created without the autoDelete event and I am not getting any error. I checked my table in phpmyadmin by executing this statement show events in the sql browser I am getting the result empty results as I said the bus table is there?!

I checked the EVENT syntax code in the phpmyadmin sql browser and it works?

DatabaseMetaData dbm = con.getMetaData();
        ResultSet tables = dbm.getTables(null, null, "bus", null);
        if (tables.next()) {
            // here if the table exist just update data. I have the most of my code her.
            return status;
        } else {
            // Create bus table
            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 AutoDelete"
                    + "ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 MINUTE "
                    + "DO "
                    + "DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE)");
            insert_update_data(mac, route, latD, longD, con);
            return status;

        }

Try putting space after the event name

"CREATE EVENT AutoDelete "

I think your code is currently creating a string that looks like :

CREATE EVENT AutoDeleteON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 MINUTE DO DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE)

Although I am surprised that this didn't throw a SQLException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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