服务器每天零点更新数据库

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <time.h>
#include <sys/time.h> 
#include <stdlib.h>
#include <signal.h>
#include <mysql.h>
#include <string>
using namespace std;

static int count = 0;
static struct itimerval oldtv;
static int state = 0;
MYSQL *conn_ptr;
int res;
void set_timer()
{
     struct itimerval itv;
     itv.it_interval.tv_sec = 60;
     itv.it_interval.tv_usec = 0;
     itv.it_value.tv_sec = 60;
     itv.it_value.tv_usec = 0;	
     setitimer(ITIMER_REAL, &itv, &oldtv);
}

void signal_handler(int m)
{
     time_t rawtime;
     struct tm * timeinfo;
     time(&rawtime);
     timeinfo = localtime(&rawtime);
     if (timeinfo->tm_hour == 0&&state==0)	{
         if (conn_ptr) {
            res = mysql_query(conn_ptr, "UPDATE miduser SET sate = 0 WHERE sate = 1");
            if (!res) {
                //输出受影响的行数
                printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(conn_ptr));
            }	else {
                //打印出错误代码及详细信息
                fprintf(stderr, "Insert error %d: %sn", mysql_errno(conn_ptr), mysql_error(conn_ptr));
            }
        }	else {
          printf("Connection failed\n");	
        }
          state += 1;
    }
    if (timeinfo->tm_hour == 1)state = 0;

int main()
{
     conn_ptr = mysql_init(NULL);
     if (!conn_ptr) 
     {
            printf("mysql_init failed\n");
            return EXIT_FAILURE;	
     }	
    conn_ptr = mysql_real_connect(conn_ptr, "115.28.xx.xx", "xxxx", "xxxx", "fuzhu", 3306, NULL, 0);
    signal(SIGALRM, signal_handler);
    set_timer();
    getchar();
    mysql_close(conn_ptr);	
    return 1;
 }
Built with Hugo
主题 StackJimmy 设计