MySQL Query select fields where time value in table is equal to current time

会有一股神秘感。 提交于 2019-12-25 08:06:20

问题


What I want to do is have a program run every minute. The program uses C code to get the current time and date and then I am trying to get a MySQL query to compare the time in the database field to see if it matches the current time. I only want to use the hour and minute when exctraing the value from MySQL and not the seconds. I can not seem to get the MySQL query to work.

Note the %s in the code is for the C program to insert the current time generated by the c code.

Here is the MySQL:

SELECT active, type, date, time, action, command FROM `Alarm` 
WHERE TIME_FORMAT('time', '%H:%i') = '%s'

WHen I print the query after snprintf trying to insert the time variable I get this as an output it seem that it is trying to insert the value into the %i and not the %s, and that the format for the time is not working:

SELECT alarm_active, alarm_category, alarm_date, alarm_time, alarm_action, alarm_command FROM Alarm WHERE TIME_FORMAT(alarm_time, '%H:6297664') = '<�F'

Here is the C code:

char buffer[1024];
    const char *query = "SELECT alarm_active, alarm_category, alarm_date, alarm_time, alarm_action, alarm_command FROM `Alarm` WHERE (alarm_time='%s')";
//const char *query = "SELECT active, type, date, time, action, command FROM `Alarm` WHERE active = '1'";


//Checking to see if connection to DB is succefful
if (mysql_query(conn, query) != 0)
{
    fprintf(stderr, "%s\n", mysql_error(conn));
    exit(1);

} else {

    if (snprintf(buffer, sizeof(buffer), query, current_time) >= sizeof(buffer))
    {
        printf("Issue with Buffer \n");
        exit (-1);

    }

Rest of the code that gets the time:

char current_time [11];

time_t raw;
time(&raw);

struct tm *time_ptr;
time_ptr = localtime(&raw);

now with the "tm", you can format it to a buffer
char current_date[11];
char current_time [11];     

strftime(current_date, sizeof(current_date), "%m/%d/%Y", time_ptr);
strftime(current_time, sizeof(current_time), "%H:%M", time_ptr);

回答1:


You don't need C code for this. You can do everything in the database:

SELECT active, type, date, time, action, command
FROM `Alarm`
WHERE TIME_FORMAT(time, '%H:%i') = TIME_FORMAT(now(), '%H:%i') and
      date = curdate();

However, I strongly advise you not to take this approach. You should store the id (or at least the date/time) of the last record processed. Then you should select all ids since then.

Your suggested method has the significant possibility of running twice in the same minute, or missing a minute.




回答2:


TIME_FORMAT('time', '%H:%i')

You put the name of your time column in single-quotes, which are string literal delimiters, not identifier delimiters. So you're trying to format the time from a literal string 'time' not the value of a column named time.

Try this:

TIME_FORMAT(`time`, '%H:%i')

You might want to read When to use single quotes, double quotes, and backticks in MySQL and https://dev.mysql.com/doc/refman/5.7/en/identifiers.html


Another consideration. You said you're using C code, and I see you have %s which I assume is a control character for sprintf(). You should inspect the SQL query after you interpolate values into it. Test that query in the MySQL command-line client or in MySQL Workbench.

Or better yet, don't use sprintf(), use parameterized queries. See http://lgallardo.com/en/2011/06/23/sentencias-preparadas-de-mysql-en-c-ejemplo-completo/ for an example of using parameterized queries in C.




回答3:


sprintf ... '%%H:%%i' ... %s

That is, hide the %H and %i from sprintf by saying that those are literal percents, not substitution places. Only the %s is to be substituted in the C code. By the time it gets to MySQL it will be this string:

... TIME_FORMAT(`time`, '%H:%i') = '12:34'


来源:https://stackoverflow.com/questions/41904422/mysql-query-select-fields-where-time-value-in-table-is-equal-to-current-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!