I'm trying to create a MySQL event so that I can have files automatically added to my MySQL database. Basically, I'm running an XAMPP server right now so all my website/database associated image files are inside the 'C:\Users\Mus\server\htdocs\images' directory. Whenever I put an image file into this directory; I need this file to be uploaded to the database automatically via this MySQL event which I'm trying to create. This means that the database needs to keep checking for new files at time intervals of say 5 seconds and if a new file has been added to said directory, a new entry will be automatically created for the image (within a specified table) and at the same time, the image will then be uploaded to that very same table within the database.
The SQL code I've used so far is:
CREATE EVENT 'eventnov'
ON SCHEDULE EVERY 5 SECOND
STARTS '2015-10-27 00:00:00'
ENDS '2015-10-28 00:00:00'
DO INSERT INTO 'images_11_nov'
SELECT NOW();
I realize lines 5 and 6 are probably completely wrong but I've tried researching existing problems and I've found no luck. I've looked at the official MySQL documentation and unfortunately I can't find anything relevant to my specific problem. I don't know the syntax to have MySQL do all the following within a single MySQL event:
- Check the specified file directory (C:\Users\Mus\server\htdocs\images) at set time intervals of 5 seconds and IF there is a newly moved image into said directory....(number 2)
- Automatically create an entry within the 'images_11_nov' table as a place holder for a new image and....(number 3)
- Automatically upload these new image files (which had recently been moved to 'C:\Users\Mus\server\htdocs\images') and insert them into the 'images_11_nov' table.
Could someone help me finish off the above code to perform these necessary functions? If I need to provide more information, please do let me know and I shall oblige.
- Edit 1: The column names for the 'images_11_nov' table are 'title' (varchar), 'imgdata' (BLOB), 'picid' (INT), 'datetime' (timestamp).
- Edit 2: I've changed the SQL code in regard to Jayesh Dhandha's answer but I'm still getting syntax errors in MySQL. The updated code is as follows:
CREATE EVENT 'eventnov'
ON SCHEDULE EVERY 5 SECOND
STARTS '2015-10-27 00:00:00'
ENDS '2015-10-28 00:00:00'
SELECT count(*) 'images_11_nov'
system(ls C:\Users\Mus\server\htdocs\images | wc -l)
system(ls -t | head -1)
DO INSERT INTO 'images_11_nov';